I am writing a program where the user needs to input a string. The problem is that the first word in the string always gets cut off and a space stays. Here is my code:
Code:
//a class has been created and a new variable called var is also present
//name is of type string
while(selection != 0){
//some menu display
cout<<"Please choose an action: ";
cin>>selection;
switch(selection){
case 1:
cout<<"Name: ";
cin>>name;
getline(cin, name);
var.addname(name);
break;
//some other cases down here
}
}
for example, if I enter name John Doe, then the String name contains " Doe" instead of the whole name. How can I fix my code to fix this?
thanks