Hi everyone,

I have a simple problem related to string, but I did not notice before that we can't give space in string input. For exmaple in the following program, take name input from user. If user enter M John, it will skip the enter email input(and the program will be terminated).

My question is, why is this happening. And what is the solution of this problem. Because I want spaces in string input. Anyone please help. Thanks !

#include <iostream>
#include <string>

int main()

   string name;
   string email;

   cout <<"Enter name : "<<endl;
   cin >> name;
   cout <<"Enter email : "<<endl;
   cin >> email;

   cout <<"\n\nProgram terminated !"<<endl;

return 0;

Recommended Answers

Use std::getline, at present when you have cin >> name, if the entered name is M John then only M will be read into the name, for information on why your program then terminates refer to

Jump to Post

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.