Doing something wrong.

So, I have just started finishing fixing up all the majority of the work, and I realized when I enter strings with spaces it doesn't work when I try to show it later on.

1
2
3
4
5
6
7
8
9
	//Input: Personal Info
	cout << "Enter the diver's name:\t";
	cin >> name;
	cin.sync(); //clear buffer

	
	cout << "\n" << "Enter the diver's city:\t";
	cin >> city;
	cin.sync(); //clear buffer 
Last edited on
This is because the stream extraction operator (>>) only gets one word, and then std::cin.sync() clears the rest of the buffer, losing it all. Instead, use std::getline():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main() {
    std::string name, city;
    // ...
    
    std::cout << "Enter the diver's name:\t";
    std::getline(std::cin, name);
    // no need for std::cin.sync() here

    std::cout << "Enter the driver's city:\n";
    std::getline(std::cin, city);
    // or here (std::getline() gets all the way to the end of the line)

    // ...
}
Last edited on
1
2
3
4
5
6
7
8
9
	//Input: Personal Info
	cout << "Enter the diver's name:\t";
	cin >> name;
	getline(cin, name); //allows full name


	cout << "\n" << "Enter the diver's city:\t";
	cin >> city;
	getline(cin, city); //allows full name 


So I changed it as you said, and it only gave me the last names. Am I doing it wrong still?
Last edited on
Yeah, you kept the cin >> name parts. Get rid of lines 19 and 24 and you should be good to go.
Oh jeez. That was stupid of me. I didn't even realize that the lines you gave me were input commands. Thank you so much! You've saved me hours of grief!
Topic archived. No new replies allowed.