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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -