cin in Loop Problem

I'm trying to make a stack application but I'm having trouble with using cin a loop that in case the user enters a different value type, it will loop again to ask for the user's input.

I have tried using cin.ignore() but still, it didn't work.

An illustration of my problem: http://i50.tinypic.com/j6o801.jpg


My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stack>
#include <cstdlib>
#include <cctype>
using namespace std;

int main()
{
    stack<int> n;
    int var,ctr=-1;
    int* start;
    char choice;
    do{
        while(1) // here goes my problem, in this loop
        {
        cout<<"Enter an integer: ";
        if(cin>>var)
            break;
        }
        n.push(var);
        if (n.size()==0)
            start=&n.top();

        cout<<"Do you want to enter another integer? (Y/N) : ";
        cin>>choice;
        system("cls");

    }while (choice=='Y' | choice=='y');

    ctr=n.size();
    cout<<"Display input(s)"<<endl;
    while (!n.empty())
    {
        for (int i=ctr-1; i>=0;i--)
        {
        int *ptr=&n.top()-i;
        cout<<*ptr<<" ";
        }
        cout<<"\nPopping.. "<<n.top();
        n.pop();
        ctr--;
        cout<<endl;
        if (ctr==0)
            cout<<"\nSTACK is now empty!";
    }

    return 0;
}
choice (line 12) is uninitialized.
If you execute the break at line 18, you will exit the inner loop and the while condition at line 28 will be evaluated against an uninitialized value, which is pretty much guaranteed to be false taking you back to the top of the loop.
But wouldn't line 25 execute first after I make the break at line 18 so there would be a value for the while statement at line 28?
Have you tried using cin.fail(); ?
(returns a boolean value [1 if user enters an incorrect data type and 0 for normal operation] )
But wouldn't line 25 execute first after I make the break at line 18

You're correct. I missed the closing brace at line 19.
I tried using cin.fail(); unfortunately, it didn't solve the problem. Have you tried running it in your compiler?
Last edited on
Line 28 is a bitwise or. You want a logical or.
 
while (choice=='Y' || choice=='y');



cin is broken, so you can't use it to ignore:
1
2
3
4
5
6
7
8
9
10
while(1) // here goes my problem, in this loop
{
  cout<<"Enter an integer: ";
  if(cin>>var)  // cin success
    break;

  // cin is broken
  cin.clear();
  cin.inore(80,'\n'); 
}


It would be "better" to formulate your loop:
while (!(cin >> var)) //...
This solved the problem:
1
2
3
4
5
6
cout<<"Enter an integer: \n";
        while(!(cin>>var))
        {
            cin.clear();
            cin.ignore(80,'\n');
        }


But, would you mind explaining me what really happened to the execution as to why I was getting that error earlier.
When you entered a non-numeric, cin couldn't remove it from cin's buffer so it stayed there along with the newline. cin.clear resets cin's error flags. cin.ignore eats everything up to and including the newline so that cin's buffer is clear for the next input.
Topic archived. No new replies allowed.