How to include white spaces in string input

In the code snippet below the output of the program is
Ahmed Anis

if I try to replace the line
name = "Ahmed Anis";

cin >> name;

and type in
Ahmed Anis

in the program input during run time it only output my first name i.e

It appears that white spaces are ignored in the input process, I was wondering if the was a way to include my full name with spaces in string input?

Thanks in advance
#include <iostream>
#include <string>
using namespace std;
void main(void)
	string name;
        name = "Ahmed Anis";
	cout << name << endl;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>>> It appears that white spaces are ignored
No, on contrary, the whitespace are recognized as terminator. >ou would need one more cin >> xxx to catch the next word.

If you want to catch a whole line you should use getline

    getline(cin, name);

Here you could type as much as you want until enter is typed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a_anis3000Author Commented:
just tried getline, its the perfect workaround for the space issue. Thanks itsmeandnobodyelse. Before closing the question, I was wondering if this could be used instead of cin for other variable types or it is just for strings?
>>>> I was wondering if this could be used instead of cin
Yes, you better make all reads using getline. However for integers or floatinng-point values you have to convert from string to int/double after. That could be done in C++ using stringstreams:

#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;

    string line;

    cout << "enter your age  ";
    getline(cin, line);

    int i = 0;
    istringstream iss(line);
    if (!iss >> i)
           cout << line << " was not a valid input " << endl;

a_anis3000Author Commented:
Perfect work around. Thanks for the tips
You can redefine the field separator with cin.setfill() function or manipulator. For example, use code

string name;
int age;
cin >> setfill(',') >> name >> setfill(' ') >> age;

to read the following line;

Ahmed Anis, 23
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.