string proplem

Hello

I m Just begin to write my first program in c++ and I have problem
string StringVar;
cin >> StringVar;
getline(cin, StringVar,'\n');
cout << "You entered" << StringVar;

when I write this sample code and run it
for example if i entered her name is sofia
the first word will not appear to me in cout
what will appear name is sofia without the word her
help me please
You are overwriting the name in StringVar because you read the input twice, once with >> and the other time with getline().
Try just using getline():
1
2
3
4
string StringVar;
// cin >> StringVar; // don't need this
getline(cin, StringVar,'\n');
cout << "You entered" << StringVar; 
Last edited on
It is work now Thank you for help
Topic archived. No new replies allowed.