-
Formatted input
I want to input two variables like a/b.One book suggest this as
Code:
int a,b;
char dummychar;
cin>>a>>dummychar>>b;
Can anyone explain why above code gives the desired result.Also is there any other way out.In C we used to do it as
Code:
scanf("%d/%d",&a,&b);
Also please give me a link to other aspect of formatted input in C++.
-
Because '/' is a character and you can't read in a character into an integer. If you did the stream would fail.
Code:
cin >> a >> b;
// a = value of a
// The stream fails at second input and all cin statements
// are ignored for the rest of the program
The dummy character is there to read the input that you don't want.