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.

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
99
100
101
102
103
104
105
106
// Includes
#include "stdafx.h"
#include <iostream>
#include <string>
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
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
#include <iostream>

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<<run1<<" came in first with a score of "<<time1<<endl;
        cout<<run2<<" came in second with a score of "<<time2<<endl;
        cout<<run3<<" came in third with a score of "<<time3<<endl;
     }
    else
     {
        cout<<run1<<" came in first with a score of "<<time1<<endl;
        cout<<run3<<" came in second with a score of "<<time3<<endl;
        cout<<run2<<" came in third with a score of "<<time2<<endl;
     }
  }
}
{ // check if 2 came first
if(time2 < time1 && time2 < time3)
  {
    if(time1 < time3) //if he did, then check to see where run1 and run3 placed
     {
        cout<<run2<<" came in first with a score of "<<time2<<endl;
        cout<<run1<<" came in second with a score of "<<time1<<endl;
        cout<<run3<<" came in third with a score of "<<time3<<endl;
     }
    else
     {
        cout<<run2<<" came in first with a score of "<<time2<<endl;
        cout<<run3<<" came in second with a score of "<<time3<<endl;
        cout<<run1<<" came in third with a score of "<<time1<<endl;
     }
  }
}
{ // check if 3 came first
if(time3 < time1 && time3 < time2)
  {
    if(time2 < time1) //if he did, then check to see where run1 and run2 placed
     {
        cout<<run3<<" came in first with a score of "<<time3<<endl;
        cout<<run2<<" came in second with a score of "<<time2<<endl;
        cout<<run1<<" came in third with a score of "<<time1<<endl;
     }
    else
     {
        cout<<run3<<" came in first with a score of "<<time3<<endl;
        cout<<run1<<" came in second with a score of "<<time1<<endl;
        cout<<run2<<" came in third with a score of "<<time2<<endl;
     }
  }
}
    system("PAUSE");
    return EXIT_SUCCESS;
}


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.