Is it possible to get a single line of int valus (0-9) using the cin >> function to be read into a stack one by one without first storing them in an array?
Like, for example, if I wanted to have:
(some created class) x;
cin >> x;
so if the input was: 012345678901234567890123456789
the stack would be filled with those numbers one digit at a time, like: push(0); push(1); push(2); etc.
I can't seem to find any way to do this other than making x an array/string and then pulling each digit out of that array one by one.
taking input directly to an ADT such as a stack.
I'd read it into a std::string and iterate through the string, but based on your question it seems you'd find that too similar to using an array very do you want to avoid using an array?
I'm doing a project for learning data structures and one of the requirements is we cannot use arrays or the standard template library. The thing is, i've got the general idea of the program down fine except for inputing the data. Isn't a string just a character array? I dunno, I may be overthinking this but if it's possible, i would like to try to do it without strings or arrays.
Since cin is an object of stream could you use the get() function to only get one number out of the buffer at a time?
http://www.cplusplus...am/istream/get/
Not sure if it will work, but looks like it could possibly work, although it may not be the most elegant way
http://www.cplusplus...am/istream/get/
Not sure if it will work, but looks like it could possibly work, although it may not be the most elegant way
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
#include <iostream>
#include <vector>
#include <cctype>
struct MyStack {
std::vector<int> v;
};
std::istream &operator>>(std::istream &is, MyStack &s) {
char c;
while (isdigit(is.peek()) && is >> c)
s.v.push_back(c-'0');
return is;
}
std::ostream &operator<<(std::ostream &os, MyStack const &s) {
for (std::vector<int>::const_iterator it = s.v.begin();
it != s.v.end(); ++it)
os << *it << ' ';
return os;
}
int main() {
MyStack x;
std::cin >> x;
std::cout << x << '\n';
}
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
Alternatively, just use std::stack Note that overloading >> for this is a damn silly idea, but...
#include <iostream>
#include <stack>
#include <cctype>
std::istream &operator>>(std::istream &is, std::stack<int> &s) {
char c;
while (isdigit(is.peek()) && is >> c)
s.push(c-'0');
return is;
}
int main() {
std::stack<int> x;
std::cin >> x;
}
I've ran into a problem doing this. Do I need to clear a buffer or something before I call cin >> a second time? My problem is, if I take code like Washu's but in a class and then call cin >> twice, it lets me input numbers for the first cin call but not the second. It skips the second call like its not even there.
for example: (>> is overloaded and is a friend of myclass)
myclass a,b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ;
cin>>b;
the program will run as:
Enter first number: 123456
Enter second number: (program exits with code 0)
for example: (>> is overloaded and is a friend of myclass)
myclass a,b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ;
cin>>b;
the program will run as:
Enter first number: 123456
Enter second number: (program exits with code 0)
I've ran into a problem doing this. Do I need to clear a buffer or something before I call cin >> a second time? My problem is, if I take code like Washu's but in a class and then call cin >> twice, it lets me input numbers for the first cin call but not the second. It skips the second call like its not even there.
for example: (>> is overloaded and is a friend of myclass)
myclass a,b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ;
cin>>b;
the program will run as:
Enter first number: 123456
Enter second number: (program exits with code 0)
I usually throw a cin.ignore(); under cin statements because cin only grabs the number off the stream, it leaves the return key on the buffer. If you use cin again directly after it'l grab the \n off the buffer and basically skip over it, making the variable blank.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement