Indeterminate Input

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.
closed account (Dy7SLyTq)
stringstreams
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.
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
@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.