Given this code:
#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
int main() {
int a;
string b;
cin >> a;
cin >> b;
return 0;
}
I tried compiling it with g++ and running it.
When assigning a char to a, at the first cin, the following instruction seems to be skipped.
Even if add two getchar() instructions between the last two lines, only the second getchar() seems to be executed.
Can somebody accurately explain what's happening at low level, which seemingly results in an apparent non execution of those lines?
EDIT:
Using this debug code:
#include <cstdio>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
int a;
string b;
cin >> a;
cin >> b;
cout << "a is "<< a << endl;
cout << "b is "<< b << endl;
getchar();
return 0;
}
INPUT 1test
OUTPUT a is 1 b is test * No getchar executed *
INPUT 1 test
OUTPUT a is 1 b is test
INPUT ttest
OUTPUT a is 0 b is
INPUT t
// Skips the second cin
OUTPUT a is 0 b is
NOTE:
getchar() was not executed even once.