Converting input to integer on one line

Basically, my intention is to do something like this:

1
2
3
4
while (stack.push(cin.get()))
  {
    cout << "\nInteger " << stack.peek() << " was put onto the stack.\n";
  }


The stack is an abstract data type I made; it's self-explanatory if you know stacks. The loop keeps adding the inputted integers until a non-integer is typed.

The problem is the cin.get(). I have no idea how to take direct command line input and make it an integer. cin.getline() doesn't work, either. And I can't do atoi(cin.get()) (or any variation) to convert cin.get()'s ASCII value to its character.

I feel braindead right now; I don't think atoi would work; there's no casting as a string...
Last edited on
Operator overloading?
1
2
3
4
5
6
7
istream& operator>>(istream& in, const Stack& stack)
{
      int i;
      in>>i;
      stack.push(i);
      return in;
}


And then use
1
2
3
4
while (cin>>stack)
  {
    cout << "\nInteger " << stack.peek() << " was put onto the stack.\n";
  }


instead.
Last edited on
That works, but it wasn't exactly what I was looking for.

Thanks. ^_^
Topic archived. No new replies allowed.