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.
#include <iostream>
#include <string>
#include <conio.h>
usingnamespace 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 -
#include <iostream>
#include <string>
#include <conio.h>
usingnamespace 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;
}