Running The race HElp!

Hi guys, this is not showing the second place!!

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {

string runner1 = "";
string runner2 = "";
string runner3 = "";
double timeR1 = 0.0;
double timeR2 = 0.0;
double timeR3 = 0.0;
string first = "";
string second = "";
string third = "";


cout << "Runner 1 name: ";
cin >> runner1;
cout << "Runner 1 Time: ";
cin >> timeR1;

cout << "Runner 2 name: ";
cin >> runner2;
cout << "Runner 2 Time: ";
cin >> timeR2;

cout << "Runner 3 name: ";
cin >> runner3;
cout << "Runner 3 Time: ";
cin >> timeR3;

while (timeR1 < 0 || timeR2 < 0 || timeR3 < 0)
{
cout << " ***Time can not be negative.***\n "
<< "***Please enter a positive number when prompt***" << endl;
cout << "Runner 1 name: ";
cin >> runner1;
cout << "Runner 1 Time: ";
cin >> timeR1;

cout << "Runner 2 name: ";
cin >> runner2;
cout << "Runner 2 Time: ";
cin >> timeR2;

cout << "Runner 3 name: ";
cin >> runner3;
cout << "Runner 3 Time: ";
cin >> timeR3;
}

if (timeR1 < timeR2 && timeR1 < timeR3)
{
first = runner1;
}
else if (timeR1 < timeR2 && timeR1 > timeR3)
{
second = runner1;
}

else if (timeR1 > timeR2 && timeR1 > timeR3)
{
third = runner1;
}



if (timeR2 < timeR1 && timeR2 < timeR3)
{
first = runner2;
}

else if (timeR2 < timeR1 && timeR2 > timeR3)
{
second = runner2;
}
else if (timeR2 > timeR1 && timeR2 > timeR3)

{
third = runner2;
}



if (timeR3 < timeR1 && timeR3 < timeR2)

{
first = runner3;
}
else if (timeR3 < timeR1 && timeR3 > timeR2)
{
second = runner3;
}
else if (timeR3 > timeR1 && timeR3 > timeR2)
{
third = runner3;
}


cout << "The Winner is: " << first << endl;
cout << "The second place is: " << second << endl;
cout << "The third place is: " << third << endl;



system("pause");
return 0;
}
First, please use code tags when posting code. See: http://www.cplusplus.com/articles/jEywvCM9/

What do you mean by "not showing second"?

You have three separate if-statements. However, there can be only one winner. All the conditions make the whole complex.


What if you would sort results first?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if ( R2 < R1 )
{
  // swap R1 and R2
  // swap runner1 and runner2
}
// now we know that runner1 was faster than runner2

if ( R3 < R1 )
{
  // swap R1 and R3
  // swap runner1 and runner3
}
// now we know that runner1 was faster than runner2 and runner3 

if ( R3 < R2 )
{
  // swap R2 and R3
  // swap runner2 and runner3
}
// now we know that runner2 was faster runner3 
Topic archived. No new replies allowed.