Indeterminate Input

Jul 8, 2013 at 6:40am
Hello!
In my program user inputs 1 or 2 integer variables(sometimes 1 and sometimes 2).

1
2
 int a, b;
 cin >> a >> b;

or
1
2
 int a;
 cin >> a;



So, when I read the first number how to check whether the next exists?

Thanks.
Jul 8, 2013 at 6:45am
closed account (Dy7SLyTq)
stringstreams
Jul 8, 2013 at 6:52am
You can't exactly read the user's mind and know they are going to input another integer, but you might be able to require that the user will separate the numbers by spaces and terminate the input with a newline. That way, you read until you hit a newline and know that once you get that, that is all the user is planning to input.
Jul 8, 2013 at 6:55am
closed account (Dy7SLyTq)
sure you can... its called testing the buffer.

edit:
i agree with your method but it is possible to do what the user is asking
Last edited on Jul 8, 2013 at 6:56am
Jul 8, 2013 at 7:18am
@Zhuge, @DTSCode: Thanks.

I've done it this way and it seems it works.
1
2
3
4
5
6
7
8
9
10
     long long a = 0, b = 0;
     char c;
     while(!((c = cin.get()) == ' ' || (c = cin.get()) == EOF)){
              a = (a * 10) + (c - 48);
     }
     bool isB = false;
     while((c = cin.get()) != EOF){
              b = (b * 10) + (c - 48);
              isB = true;
     }
Topic archived. No new replies allowed.