String Input Error

Can someone please assist with an input of a string error.

Here is my code
cout<< "Please enter the following information: " << endl;
cout << "CustomerName"<<endl;
cin >> CustomerName;
cout << "CustomerNumber"<<endl;
cin >> CustomerNumber;

If I input Mrs Smith with a space, it doesn't not prompt me to enter a CustomerNumber, it skips the code. But if I input Mrs.Smith with no spaces it prompt me for the customer number.
I have change CustomerName from a string to a char CustomerName[10] but with no luck.
Any assistance would be appreciated.
Space is considered to be a delimiter, so it reads the first part "Mrs" into CustomerName and keeps the second part "Smith" in the buffer.

Then it tries to put the leftover "Smith" into CustomerNumber, which doesn't make sense (assuming CustomerNumber is int) and then the stream std::cin goes into an error state.

I have change CustomerName from a string to a char CustomerName[10] but with no luck.

Don't do that. I strongly recommend going back to std::string.

All you have to do differently is to use std::getline() like so:

getline(cin, CustomerName); // to input spaces as well

http://www.cplusplus.com/reference/string/string/getline/
Thanks it worked :)
Topic archived. No new replies allowed.