//Diego Martinez CSC5 Chapter 4, P.223,#14
/*******************************************************************************
* RANKING RUNNERS BY FASTEST TIME
* ______________________________________________________________________________
* The program asks the user to enter the names of three runners and the time
* each one took to finish a race. It ensures that all entered times are positive
* numbers by validating the input and prompting the user again if an invalid
* value is entered.
*
*
* Computation is based on the Formula:
* Order runners by increasing time:
* smallest → 1st
* middle → 2nd
* largest → 3rd
* ____________________________________________________________________________
* INPUT
* Runner 1 : name and time finished
* Runner 2 : name and time finished
* Runner 3 : name and time finished
* OUTPUT
* 1st Place : runner with the fastest time
* 2nd Place : runner with the second fastest time
* 3ed Place : runner with the slowest time
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main() {
string name1, name2, name3;
double time1, time2, time3;
// Input names
cout << "Enter the name of runner 1: ";
getline(cin, name1);
cout << "Enter the name of runner 2: ";
getline(cin, name2);
cout << "Enter the name of runner 3: ";
getline(cin, name3);
// Input times with validation
cout << "Enter the time for " << name1 << ": ";
cin >> time1;
while (time1 <= 0) {
cout << "Invalid input. Time must be a positive number. Enter again: ";
cin >> time1;
}
cout << "Enter the time for " << name2 << ": ";
cin >> time2;
while (time2 <= 0) {
cout << "Invalid input. Time must be a positive number. Enter again: ";
cin >> time2;
}
cout << "Enter the time for " << name3 << ": ";
cin >> time3;
while (time3 <= 0) {
cout << "Invalid input. Time must be a positive number. Enter again: ";
cin >> time3;
}
// Determine order
string first, second, third;
if (time1 <= time2 && time1 <= time3) {
first = name1;
if (time2 <= time3) {
second = name2;
third = name3;
} else {
second = name3;
third = name2;
}
}
else if (time2 <= time1 && time2 <= time3) {
first = name2;
if (time1 <= time3) {
second = name1;
third = name3;
} else {
second = name3;
third = name1;
}
}
else {
first = name3;
if (time1 <= time2) {
second = name1;
third = name2;
} else {
second = name2;
third = name1;
}
}
// Output results
cout << "\nRace Results:\n";
cout << "1st place: " << first << endl;
cout << "2nd place: " << second << endl;
cout << "3rd place: " << third << endl;
return 0;
}