If Statements

When I compile this it skips the over my error checking and ends the program. Can anyone point out what I am doing wrong?



#include <iostream>
#include <string>

using namespace std;

int main()
{

/*
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?)
*/
string racer1, racer2, racer3;
double r1, r2, r3;

cout << "Enter the name of Racer 1: ";
getline(cin, racer1);
cout << "Enter the name of Racer 2: ";
getline(cin, racer2);
cout << "Enter the name of Racer 3: ";
getline(cin, racer3);
cout << endl;

cout << "Enter the time for " << racer1 << " : ";
cin >> r1;
cout << "Enter the time for " << racer2 << " : ";
cin >> r2;
cout << "Enter the time for " << racer3 << " : ";
cin >> r3;
cin.ignore();

if (r1 > r2)
{
if (r1 > r3)
{
if(r2 > r3)
{
cout << racer1 << ": " << r1 << endl;
cout << racer2 << ": " << r2 << endl;
cout << racer3 << ": " << r3 << endl;
}
else
{
cout << racer1 << ": " << r1 << endl;
cout << racer3 << ": " << r3 << endl;
cout << racer2 << ": " << r2 << endl;
}
}
}
else if(r2 > r1)
{
if (r2 > r3)
{
if (r1 > r3)
{
cout << racer2 << ": " << r2 << endl;
cout << racer1 << ": " << r1 << endl;
cout << racer3 << ": " << r3 << endl;
}
else
{
cout << racer2 << ": " << r2 << endl;
cout << racer3 << ": " << r3 << endl;
cout << racer1 << ": " << r1 << endl;
}
}
}
else if (r3 > r1)
{
if(r3 > r2)
{
if (r1 > r2)
{
cout << racer3 << ": " << r3 << endl;
cout << racer1 << ": " << r1 << endl;
cout << racer2 << ": " << r2 << endl;
}
else
{
cout << racer3 << ": " << r3 << endl;
cout << racer2 << ": " << r2 << endl;
cout << racer1 << ": " << r1 << endl;
}
}
}








return 0;
}
I threw in some output statements to trace where the code was going in the nested ifs. There's at least one case where the times get picked up in one of the else ifs but gets disqualified from the cout statements.

Are you listing the person with the longest time first or the shortest time first?

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string racer1, racer2, racer3;
    double r1, r2, r3;
    
    cout << "Enter the name of Racer 1: ";
    getline(cin, racer1);
    cout << "Enter the name of Racer 2: ";
    getline(cin, racer2);
    cout << "Enter the name of Racer 3: ";
    getline(cin, racer3);
    cout << endl;
    
    cout << "Enter the time for " << racer1 << " : ";
    cin >> r1;
    cout << "Enter the time for " << racer2 << " : ";
    cin >> r2;
    cout << "Enter the time for " << racer3 << " : ";
    cin >> r3;
    cin.ignore();
    
    if (r1 > r2)
    {
        cout << "r1>r3\n";
        if (r1 > r3)
        {
            cout << "r1>r3\n";
            if(r2 > r3)
            {
                cout << "r2>r3\n";
                cout << racer1 << ": " << r1 << endl;
                cout << racer2 << ": " << r2 << endl;
                cout << racer3 << ": " << r3 << endl;
            }
            else
            {
                cout << "r2<=r3\n";
                cout << racer1 << ": " << r1 << endl;
                cout << racer3 << ": " << r3 << endl;
                cout << racer2 << ": " << r2 << endl;
            }
        }
    }
    else if(r2 > r1)
    {
        cout << "r2>r1\n";
        if (r2 > r3)
        {
            cout << "r2>r3\n";
            if (r1 > r3)
            {
                cout << "r1>r3\n";
                cout << racer2 << ": " << r2 << endl;
                cout << racer1 << ": " << r1 << endl;
                cout << racer3 << ": " << r3 << endl;
            }
            else
            {
                cout << "r1<=r3\n";
                cout << racer2 << ": " << r2 << endl;
                cout << racer3 << ": " << r3 << endl;
                cout << racer1 << ": " << r1 << endl;
            }
        }
    }
    else if (r3 > r1)
    {
        cout << "r3>r1\n";
        if(r3 > r2)
        {
            cout << "r3>r2\n";
            if (r1 > r2)
            {
                cout << "r1>r2\n";
                cout << racer3 << ": " << r3 << endl;
                cout << racer1 << ": " << r1 << endl;
                cout << racer2 << ": " << r2 << endl;
            }
            else
            {
                cout << "r1<=r2\n";
                cout << racer3 << ": " << r3 << endl;
                cout << racer2 << ": " << r2 << endl;
                cout << racer1 << ": " << r1 << endl;
            }
        }
    }
    
    return 0;
}


Enter the name of Racer 1: r1
Enter the name of Racer 2: r2
Enter the name of Racer 3: r3

Enter the time for r1 : 500
Enter the time for r2 : 550
Enter the time for r3 : 600
r2>r1
Last edited on
Thanks! I just had to changed the all the "Greater than" signs to "Lesser than." I realized that I was looking to display the shortest time, when my coding was actually trying to sort for the longest. So the time inputs did not fall under any of the conditions given. Thanks again!
Topic archived. No new replies allowed.