Race Program

Hello, simple question here. When I compile my program, it seems to fall through and not prompt the user for the names of the second and third runners. Lines 25, and 31 are where I prompt the user to enter the name however, it only allows you enter in the name of the first runner. Any ideas? Everything else seems to work fine, It'll display the times as far as who came in first, second, and third.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

void welcome_message() {//Display message to user

	cout << "Hello user, today we are going to collect the names of three runners and their times " << endl;
	cout << "We will then display who came in first, second, and finally third." << endl;
}

int user_input() {
	string run1, run2, run3;
	double time1, time2, time3;
	int x = 0;

	cout << "What is the name of the first runner?" << endl;
	getline(cin, run1);
	
	cout << "How long did he run for?" << endl;
	cin >> time1;
	
	cout << "What is the name of the second runner?" << endl;
	getline(cin, run2);
	
	cout << "How long did he run for?" << endl;
	cin >> time2;
	
	cout << "What is the name of the third runner?" << endl;
	getline(cin, run3);
	
	cout << "How long did he run for?" << endl;
	cin >> time3;

	if (time1 > time2 && time1 > time3) { //Check first for time1

		if (time2 > time3) { //Checks second place and third

			cout << "First: " << run1 << " With a time of: " << time1 << endl;
			cout << "Second: " << run2 << " With a time of: " << time2 << endl;
			cout << "Third: " << run3 << " With a time of: " << time3 << endl;
		}
		else {

			cout << "First: " << run1 << " With a time of: " << time1 << endl;
			cout << "Second: " << run3 << " With a time of: " << time3 << endl;
			cout << "Third: " << run2 << " With a time of: " << time2 << endl;


		}
	}
	if (time2 > time1 && time2 > time3) { //Check first for time2
		if (time1 > time3) {
			cout << "First: " << run2 << " With a time of: " << time2 << endl;
			cout << "Second: " << run1 << " With a time of: " << time1 << endl;
			cout << "Third: " << run3 << " With a time of: " << time3 << endl;

		}
		else {
			cout << "First: " << run2 << " With a time of: " << time2 << endl;
			cout << "Second: " << run3 << " With a time of: " << time3 << endl;
			cout << "Third: " << run1 << " With a time of: " << time1 << endl;

		}

	}
	if (time3 > time1 && time3 > time2) { //Check first for time3
		if (time1 > time2) {
			cout << "First: " << run3 << " With a time of: " << time3 << endl;
			cout << "Second: " << run1 << " With a time of: " << time1 << endl;
			cout << "Third: " << run2 << " With a time of: " << time2 << endl;

		}
		else {
			cout << "First: " << run3 << " With a time of: " << time3 << endl;
			cout << "Second: " << run2 << " With a time of: " << time2 << endl;
			cout << "Third: " << run1 << " With a time of: " << time1 << endl;
		}

	}
	return x;
}


int main() {
	welcome_message();
	user_input();

	_getch();
	return 0;
}


Not sure why it displays the message, but then doesn't allow for user input.
That is becasue, when you switch between getline and cin. You are mixing formatted and unformatted input. Cin leaves an empty line in the stream so you have to add a cin.ignore() after it.

Just google "c++ Skipping cin/getline" And you'll get lots of results -

Look at line 24 and 32.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

void welcome_message() {//Display message to user

	cout << "Hello user, today we are going to collect the names of three runners and their times " << endl;
	cout << "We will then display who came in first, second, and finally third." << endl;
}

int user_input() {
	string run1, run2, run3;
	double time1, time2, time3;
	int x = 0;

	cout << "What is the name of the first runner?" << endl;
	getline(cin, run1);

	cout << "How long did he run for?" << endl;
	cin >> time1;

	cin.ignore(); // Added cin.ignore();

	cout << "What is the name of the second runner?" << endl;
	getline(cin, run2);

	cout << "How long did he run for?" << endl;
	cin >> time2;

	cin.ignore(); // Added cin.ignore();

	cout << "What is the name of the third runner?" << endl;
	getline(cin, run3);

	cout << "How long did he run for?" << endl;
	cin >> time3;

	

	if (time1 > time2 && time1 > time3) { //Check first for time1

		if (time2 > time3) { //Checks second place and third

			cout << "First: " << run1 << " With a time of: " << time1 << endl;
			cout << "Second: " << run2 << " With a time of: " << time2 << endl;
			cout << "Third: " << run3 << " With a time of: " << time3 << endl;
		}
		else {

			cout << "First: " << run1 << " With a time of: " << time1 << endl;
			cout << "Second: " << run3 << " With a time of: " << time3 << endl;
			cout << "Third: " << run2 << " With a time of: " << time2 << endl;


		}
	}
	if (time2 > time1 && time2 > time3) { //Check first for time2
		if (time1 > time3) {
			cout << "First: " << run2 << " With a time of: " << time2 << endl;
			cout << "Second: " << run1 << " With a time of: " << time1 << endl;
			cout << "Third: " << run3 << " With a time of: " << time3 << endl;

		}
		else {
			cout << "First: " << run2 << " With a time of: " << time2 << endl;
			cout << "Second: " << run3 << " With a time of: " << time3 << endl;
			cout << "Third: " << run1 << " With a time of: " << time1 << endl;

		}

	}
	if (time3 > time1 && time3 > time2) { //Check first for time3
		if (time1 > time2) {
			cout << "First: " << run3 << " With a time of: " << time3 << endl;
			cout << "Second: " << run1 << " With a time of: " << time1 << endl;
			cout << "Third: " << run2 << " With a time of: " << time2 << endl;

		}
		else {
			cout << "First: " << run3 << " With a time of: " << time3 << endl;
			cout << "Second: " << run2 << " With a time of: " << time2 << endl;
			cout << "Third: " << run1 << " With a time of: " << time1 << endl;
		}

	}
	return x;
}


int main() {
	welcome_message();
	user_input();

        _getch();
	return 0;
}
Last edited on
Line 22 doesn't catch the \n character and line 25 is picking it up.
Thank you Tarik, that is exactly what I was looking for. Thank you!
Topic archived. No new replies allowed.