C++ Programing question Write a program that asks the user the names of four run
ID: 3581625 • Letter: C
Question
C++ Programing question
Write a program that asks the user the names of four runners and the time it took each of them to finish a race. The program should display who came in first, second third place, and fourth place. The program should also consider the possibility of two or more runners finishing with the same time. The runners' times are between 1 and 10. The runners' names however are unique.
Note: if all the runners have the same time then all the runners come in first places.
Note: think about how you can use the dual sort function to solve this problem.
Please using the vector and dual sort function!!
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
// Main Function
int main()
{
string name1 , name2 , name3 , name4;
float time1 , time2 , time3 , time4;
double first , second , third , fourth;
cout << "This displays who came 1st , 2nd, 3rd & 4th ";
cout << "Enter name1: ";
getline(cin , name1);
cout << "Enter name2: ";
getline(cin , name2);
cout << "Enter name3: ";
getline(cin , name3);
cout << "Enter name4: ";
getline(cin , name4);
cout << "Enter time1: ";
cin >> time1;
cout << "Enter time2: ";
cin >> time2;
cout << "Enter time3: ";
cin >> time3;
cout << "Enter time4: ";
cin >> time4;
while (time1 < 0 || time2 < 0 || time3 < 0 || time4 < 0)
{
cout << " The times must be greater than 0. ";
cout << "Enter name1: ";
getline(cin , name1);
cout << "Enter name2: ";
getline(cin , name2);
cout << "Enter name3: ";
getline(cin , name3);
cout << "Enter name4: ";
getline(cin , name4);
cout << "Enter time1: ";
cin >> time1;
cout << "Enter time2: ";
cin >> time2;
cout << "Enter time3: ";
cin >> time3;
cout << "Enter time4: ";
cin >> time4;
}
cout.setf(ios_base::fixed , ios_base::floatfield);
cout.precision(1);
cout << " ";
if (time1 < time2 && time1 < time3 && time1 < time4)
{
first = time1;
cout << first << endl;
cout << name1 << " came in 1st with a time of " << first << endl;
}
else if (time2 < time1 && time2 < time3 && time2 < time4)
{
first = time2;
cout << name2 << " came in 1st with a time of " << first << endl;
}
else if (time3 < time1 && time3 < time2 && time3 < time4)
{
first = time3;
cout << name3 << " came in 1st with a time of " << first << endl;
}
else if (time4 < time1 && time4 < time2 && time4 < time3)
{
first = time4;
cout << name4 << " came in 1st with the time of " << first << endl;
}
if (time1 < time3 && time1 > time2 && time1 < time4)
{
second = time1;
cout << name1 << " came in 2nd with a time of " << second << endl;
}
else if (time2 < time3 && time2 > time1 && time2 < time4)
{
second = time2;
cout << name2 << " came in 2nd with a time of " << second << endl;
}
else if (time3 < time2 && time3 > time1 && time3 < time4 )
{
second = time3;
cout << name3 << " came in 2nd with a time of " << second << endl;
}
else if (time4 < time3 && time4 > time1 && time4 < time2)
{
second = time4;
cout << name4 << " came in 2nd with a time of " << second << endl;
if (time1 > time2 && time1 > time3 && time1 > time4)
{
third = time1;
cout << name1 << " came in 3rd with a time of " << third << endl;
}
else if (time2 > time1 && time2 > time3 && time2 > time4)
{
third = time2;
cout << name2 << " came in 3rd with a time of " << third << endl;
}
else if (time3 > time1 && time3 > time2 && time3 > time4)
{
third = time3;
cout << name3 << " came in 3rd with a time of " << third << endl;
}
else if(time4 > time1 && time4 > time2 && time4 > time3)
{
third = time3;
cout << name4 << " came in 3rd with a time of " << third << endl;
}
cout << endl << "Press ENTER to exit...";
cin.clear();
cin.sync();
cin.get();
return 0;
}
}
output:
This displays who came 1st , 2nd, 3rd & 4th
Enter name1:
Enter name2:
Enter name3:
Enter name4:
Enter time1:
Enter time2:
Enter time3:
Enter time4:
came in 1st with the time of 0.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.