This is my code and when it gets to the output for High and low score, it gives me the second to high score and 0 for the low.
I have no idea why.
can someone please guide me in the right direction?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// declare and intialize variables
string name = " ";
string city = " ";
char again = 'y';
int numberOfDivers = 0;
int judges = 0;
double score = 0.0;
double overall = 0.0;
double highest = 0.0;
double lowest = 0.0;
double total = 0.0;
do
{
cout << "Enter the diver's name: ";
getline(cin, name);
cout << "Enter the diver's city: ";
getline(cin, city);
cout << "Enter the score given by Judge #1: ";
cin >> score;
while(score < 0 || score > 10)
{
cout << "Invalid score - Please reenter (Valid range: 1 - 10) " <<endl;
cout << "Enter a score between 0 and 10 : ";
cin >> score;
}
total += score;
cout << "Enter the score given by Judge #2: ";
cin >> score;
while(score < 0 || score > 10)
{
cout << "Invalid score - Please reenter (Valid range: 1 - 10) " <<endl;
cout << "Enter a score between 0 and 10 : ";
cin >> score;
}
total += score;
cout << "Enter the score given by Judge #3: ";
cin >> score;
while(score < 0 || score > 10)
{
cout << "Invalid score - Please reenter (Valid range: 1 - 10) " <<endl;
cout << "Enter a score between 0 and 10 : ";
cin >> score;
}
total += score;
cout << "Enter the score given by Judge #4: ";
cin >> score;
while(score < 0 || score > 10)
{
cout << "Invalid score - Please reenter (Valid range: 1 - 10) " <<endl;
cout << "Enter a score between 0 and 10 : ";
cin >> score;
}
total += score;
cout << "Enter the score given by Judge #5: ";
cin >> score;
while(score < 0 || score > 10)
{
cout << "Invalid score - Please reenter (Valid range: 1 - 10) " <<endl;
cout << "Enter a score between 0 and 10 : ";
cin >> score;
}// while
double difficulty;
cout << "What was the degree of difficulty?: ";
cin >> difficulty;
while (difficulty < 1 || difficulty > 1.67)
{
cout << "Invalid number course difficulty is 1 - 1.67 please renter a number: ";
cin >> difficulty;
}
cout << "Diver's name is: " << name << endl;
cout << "The City is: " << city << endl;
double sum = 0.0;
double high = 0.0;
double low = 0.0;
for (double score = 0; score < 10; score++)
{
// test if num is the highest
if (score > high)
high = score;
// test if num is the lowest
if (score < low)
low = score;
sum += score;
}// end for
cout << endl << endl;
// drop the highest and the lowest
// sum = sum - high - low;
sum -= high;
sum -= low;
cout << setprecision(2) << fixed;
cout << "High was " << high << endl;
cout << "Low was " << low << endl;
cout << "Your total is " << sum << endl;
// Example of doing a calculation in the output statement
cout << "The average is " << sum * difficulty/3.0 << endl;
cin.ignore(); // flushes the input buffer
}while ( again == 'y' || again == 'Y');
return 0;
This is the near vicinity of the problem, and it is possibly a simple solution, but I dont seem to find it.
double sum = 0.0;
double high = 0.0;
double low = 0.0;
for (double score = 0; score < 10; score++)
{
// test if num is the highest
if (score > high)
high = score;
// test if num is the lowest
if (score < low)
low = score;
sum += score;
}// end for
cout << endl << endl;
// drop the highest and the lowest
// sum = sum - high - low;
sum -= high;
sum -= low;
cout << setprecision(2) << fixed;
cout << "High was " << high << endl;
cout << "Low was " << low << endl;
cout << "Your total is " << sum << endl;
// Example of doing a calculation in the output statement
cout << "The average is " << sum * difficulty/3.0 << endl
for (double score = 0; score < 10; score++) {
// test if num is the highest
if (score > high)
high = score;
// test if num is the lowest
if (score < low)
low = score;
sum += score;
}
Take a look at your loop (next time please use [ code ] [ /code ] tags (minus the spaces) to keep indentation). It ignores any input the program has received so far, then it loops through values from 0 to 9 and tests them against low and high.