Sign in to follow this  
Kevn

taking input directly to an ADT such as a stack.

Recommended Posts

Kevn    166
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.

Share this post


Link to post
Share on other sites
yckx    1298
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?

Share this post


Link to post
Share on other sites
Kevn    166
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.

Share this post


Link to post
Share on other sites
bd36    118
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?

[url="http://www.cplusplus.com/reference/iostream/istream/get/"]http://www.cplusplus...am/istream/get/[/url]

Not sure if it will work, but looks like it could possibly work, although it may not be the most elegant way

Share this post


Link to post
Share on other sites
alvaro    21246
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
[code]#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';
}
[/code]

Share this post


Link to post
Share on other sites
Washu    7829
[quote name='alvaro' timestamp='1334174605' post='4930351']
Here's one way to do it (I use std::vector internally for simplicity; I know you can't do that):
[/quote]

Alternatively, just use std::stack [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] Note that overloading >> for this is a damn silly idea, but...
[code]#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;
}
[/code]

Share this post


Link to post
Share on other sites
Kevn    166
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)

Share this post


Link to post
Share on other sites
Satharis    2444
[quote name='Kevn' timestamp='1334302853' post='4930889']
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)
[/quote]
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.

Share this post


Link to post
Share on other sites
alvaro    21246
Perhaps operator>> should be modified to do something closer to what it does for standard types (e.g., std::string). It probably should skip over whitespace. Not that overloading an operator to do this was a good idea to begin with or anything...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this