Currently my program will close after I input my racer's names and time.I was wondering if anyone could tell what I am doing wrong and give me some tips. There are a couple rules I have to follow for this program like no No Arrays or Vectors
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
welcome();
getRaceTimes(racer1, time1);
getRaceTimes(racer2, time2);
getRaceTimes(racer3, time3);
findWinner( racer1, racer2, racer3, time1, time2, time3);
double result = raceAverage(time1, time2, time3);
cout << endl;
cout << "The overall average race time is" << " " << result;
return 0;
system("pause");
}
void getRaceTimes(string &racername, double &racertime)
{
cout << "Please enter racer's name" << endl;
cin >> racername;
do
{
cout <<"Please enter racer's time" << endl;
cin >> racertime;
if (racertime <= 0)
cout << "Im sorry that is an invalid time. Please re-enter a value greater than 0" << endl;
} while (racertime <= 0);
}
void findWinner(string racer1, string racer2, string racer3, double time1, double time2, double time3)
{
if ((time1 < time2) && (time1 < time3))
{
cout << "Congratulations " << racer1 << " is the winner!" << endl;
cout << "Your winning time is" << time1 << endl;
}
else if ((time2 < time3) && (time2 < time1))
{
cout << "Congratulations " << racer2 << " is the winner!" << endl;
cout << "Your winning time is" << time2 << endl;
}
else if ((time3 < time1) && (time3 < time2))
{
cout << "Congratulations " << racer3 << " is the winner!" << endl;
cout << "Your winning time is" << time3 << endl;
}
else if ((time1 == time2) && (time1, time2 < time3))
{
cout << " We have a tie " << racer1 << " and " << racer2 << " win!" << endl;
cout << "Your winning time is" << time1 << endl;
}
else if ((time1 == time3) && (time1, time3 < time2))
{
cout << " We have a tie " << racer1 << " and " << racer3 << " win!" << endl;
cout << "Your winning time is" << time1 << endl;
}
else if ((time2 == time3) && (time2, time3 < time1))
{
cout << " We have a tie " << racer2 << " and " << racer3 << " win!" << endl;
cout << "Your winning time is" << time2 << endl;
}
else ((time1 == time2) && (time1 == time3));
{
cout << "We have a 3 way tie!! No winner for this race." << endl;
cout << "Your winning time is" << time1 << endl;
}
return;
}
void welcome()
{
cout << "*****************************************************************" << endl;
cout << " Welcome to race results program" << endl;
cout << " you are asked to enter the three racer’s names" << endl;
cout << " and their associated race time." << endl;
cout << " Please enter a real number for Race Time (the Race Time Must be > 0). " << endl;
cout << " Program Developed by : " << endl;
cout << "*****************************************************************" << endl;
does it work if you add this to the end of main (right at the return statement)
int stopper;
cin >> stopper;
if so, open a console and run the program from there, or do the above, or read the massive thread at the top of the beginners forum. If you don't know how to open a console and get to your program, you should learn at least a handful of dos and unix commands both, they will serve you well, do you need to see this?
@jonnin, actually in OP's main() there’s system("pause");, but it’s after return 0; :)
- - -
alexandrap98 wrote:
I was wondering if anyone could tell what I am doing wrong
Yes: your compiler can, and as a matter of fact it’s trying as hard as it can:
compiler wrote:
In function 'void findWinner(std::string, std::string, std::string, double, double, double)':
60:46: warning: left operand of comma operator has no effect [-Wunused-value]
65:46: warning: left operand of comma operator has no effect [-Wunused-value]
70:46: warning: left operand of comma operator has no effect [-Wunused-value]
It means comma operator is of no avail
here: elseif ((time1 == time2) && (time1, time2 < time3))
and here: elseif ((time1 == time3) && (time1, time3 < time2))
and here: elseif ((time2 == time3) && (time2, time3 < time1))
or, in beginner speak,
conditions are explicit. the compiler does not get human like if a,b,c < d. It only gets a < d && b<d && c<d spelled out.
else if is not a thing in c++ but many people put them together to chain. Its really 2 distinct if block /statements, no matter how it looks in the code, and the second if is inside the else part of the first if. So they are nested, but distinct.
simplifying a bunch of related if statements takes practice. For now, do them explicitly. Later you can work on your boolean math skills to trim the fat.