c++ - Why does my program immediately terminate after execution -
so i'm using mvs , wrote following program
// istream::ignore example #include <iostream> using std::cout; using std::cin; using std::endl; int main() { char first, middle, last; std::cout << "please, enter first name , surname: "; first = cin.get(); // 1 character cin.ignore(256, ' '); // ignore until space middle = cin.get(); //get 1 character cin.ignore(256, ' '); //ignore until space last = std::cin.get(); // 1 character std::cout << "your initials " << first << middle << last << endl; cin.get(); return 0; }
the program runs should, executes shuts down. please explain making program this, , how fix it?
the last cin.get() eating new line character last = std::cin.get();
, automatically gets passed on.
add cin.ignore(256, '\n');
after last = std::cin.get();
line
or use dummy cin.get();
:
cin.get(); cin.get(); return 0;
Comments
Post a Comment