### Running the race

Running the Race

Write a program that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Think about how many test cases are needed to verify that your problem works correctly. (That is, how many different finish orders are possible?)
Input Validation: Only accept positive numbers for the times.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106`` ``````// Includes #include "stdafx.h" #include #include using namespace std; // Main Function int main() { string name1 , name2 , name3; float time1 , time2 , time3; double first , second , third; cout << "This displays who came 1st , 2nd, 3rd.\n\n"; cout << "Enter name1: "; getline(cin , name1); cout << "Enter name2: "; getline(cin , name2); cout << "Enter name3: "; getline(cin , name3); cout << "Enter time1: "; cin >> time1; cout << "Enter time2: "; cin >> time2; cout << "Enter time3: "; cin >> time3; while (time1 < 0 || time2 < 0 || time3 < 0) { cout << "\n\nThe times must be greater than 0.\n\n"; cout << "Enter name1: "; getline(cin , name1); cout << "Enter name2: "; getline(cin , name2); cout << "Enter name3: "; getline(cin , name3); cout << "Enter time1: "; cin >> time1; cout << "Enter time2: "; cin >> time2; cout << "Enter time3: "; cin >> time3; } cout.setf(ios_base::fixed , ios_base::floatfield); cout.precision(1); cout << "\n\n"; if (time1 < time2 && time1 < time3) { first = time1; cout << first << endl; cout << name1 << " came in 1st with a time of " << first << endl; } else if (time2 < time1 && time2 < time3) { first = time2; cout << name2 << " came in 1st with a time of " << first << endl; } else if (time3 < time1 && time3 < time2) { first = time3; cout << name3 << " came in 1st with a time of " << first << endl; } if (time1 < time3 && time1 > time3) { second = time1; cout << name1 << " came in 2nd with a time of " << second << endl; } else if (time2 < time3 && time2 > time1) { second = time2; cout << name2 << " came in 2nd with a time of " << second << endl; } else if (time3 < time2 && time3 > time1) { second = time3; cout << name3 << " came in 2nd with a time of " << second << endl; } if (time1 > time2 && time1 > time3) { third = time1; cout << name1 << " came in 3rd with a time of " << third << endl; } else if (time2 > time1 && time2 > time3) { third = time2; cout << name2 << " came in 3rd with a time of " << third << endl; } else if (time3 > time1 && time3 > time2) { third = time3; cout << name3 << " came in 3rd with a time of " << third << endl; } cout << endl << "Press ENTER to exit..."; cin.clear(); cin.sync(); cin.get(); return 0; }``````

Heres my code, is there an easier way to do this, and my second place isnt being outputed , my first and third place is does anyone know why.
Last edited on
also my next 2 exercises are these

16. The Speed of Sound
The speed of sound varies depending on the medium through which it travels. In general, sound travels fastest in rigid media, such as steel, slower in liquid media, such as water, and lowest of all in gases, such as air. The following table shows the approximate speed of sound, measured in feet per second, in air, water, and steel.

Medium Speed (FT Per Second)
Air 1,100
Water 4,900
Steel 16,400

Write a program that displays a menu allowing the user to select air water, or steel. After
the user has made a selection, the number of feet a sound wave will travel in the selected
medium should be entered. The program will then display the amount of time it will take.
(Round the answer to four decimal places.)
Input Validation: Check that the user has selected one of the available menu choices.
Do not accept distances less than 0.

Im wondering if there is a formula i need to use, if so what is it.
T = d/r

T = Time
d = distance
r = rate

In this case the user enters the distance (in feet, since the speed is given as feet)

and the rate is equivalent to what medium the user chooses.
Ok, thank you, and what about my running the race, my second place isnt being outputted but my first and third is.
nvm i found the runniing the race problem , my first if then statement was wrong for 2nd place.
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include using namespace std; int main() { string run1, run2, run3; double time1, time2, time3; cout << " Enter the name of the runners and their time.\n Seperate name and time with a space."; cout << "\n Enter the name of runner 1 and the time: "; cin >> run1 >> time1; cout << " Enter the name of runner 2 and the time: "; cin >> run2 >> time2; cout << " Enter the name of runner 3 and the time: "; cin >> run3 >> time3; { //check if runner one came in first if(time1 < time2 && time1 < time3) { if(time2 < time3) //if he did, then check to see where run2 and run3 placed { cout<

this is how i did it, with a lil help from pogrady in another post that i saw.
i got to do that speed of sound one too, only, its program 18 for me, so i got 2 more before i do that one.
Last edited on
Topic archived. No new replies allowed.