cout << "Input street number: ";
cin >> streetnum;
cout << "Input street name: ";
cin >> streetname;
cout << "Input resource name: ";
cin >> rName;
cout << "Input architectural style: ";
cin >> aStyle;
cout << "Input year built: ";
cin >> year;
The problem with the above code happens if you enter in spaces between words. For example if I enter “Ampitheater Parkway” for streetname, then it puts “Ampitheater” in streetname, skips the prompt for resource name and enters “Parkway” into the next field. How can I fix this?
That’s because when you use the extraction operator with a string as the right-hand side, it stops at the first white space character.
What you want is the
getlinefree function:You can specify some other delimiter if you want:
Even better is to make a little function to use:
🙂