### Calculating the average score

I am going crazy with my code...but I can not find why the program is not calculating the average score at the end of the program. It is suppose to work where at the end of the program it take the number of diver input and divide it by the overallScore.

`overAllNumbers = (totalAverage / totalDivers);`

this is the whole code i wrote...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include #include using namespace std; void main() { //--------------Data of users name and city under string string diverName, diverCity; //--------------variables for char and int char mainLoop = 'y' || 'Y', mainLoopNo = 'n' || 'N'; int loop = 1, totalDivers = 0; //--------------Numbers variables double judgeScores = 1; // judges score counters double scoreInput, averageNumbers, overAllNumbers, total = 0, totalAverage = 0; // total scores and total average double degreeOfDifficulty = 0, max = 0, min = 0; // highest and lowest scores //-----------------------Write report heading------------------------------- cout << " ---------------------------------------------------------" << endl; cout << " | A Diver Score Program |" << endl; cout << " | Diving competition score from the judges |" << endl; cout << " ---------------------------------------------------------" << endl; //-----------Main loop code while(mainLoop == 'y' || 'Y') { //-----code for when loop is reset at the end diverName.clear(); diverCity.clear(); //----Counter and loop are reset to 1 judgeScores = 1; loop = 1; //----------------------------Inputs Name and city totalDivers ++; cout << "\nEnter the diver's name: "; cin >> diverName; cout << "Enter the diver's city: "; cin >> diverCity; cout << "\n"; IfError1: cout << "Enter the score given by judge #" << judgeScores << ": "; cin >> scoreInput; if(scoreInput <= 0 || scoreInput > 10) { cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl; // display invalid error msg goto IfError1; } min = scoreInput; max = scoreInput; total = scoreInput; do { judgeScores++; IfError2: cout << "Enter the score given by judge #" << judgeScores << ": "; cin >> scoreInput; if(scoreInput <= 0 || scoreInput > 10) { cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl; // display invalid error msg goto IfError2; } if(scoreInput > max) max = scoreInput; if(scoreInput < min) min = scoreInput; total = scoreInput + total; loop++; }while(loop < 5); //-----------------------Input and validate the degree of difficulty--------------------------------- IfError3: cout << "\nWhat was the degree of difficulty?: "; // Input difficulty cin >> degreeOfDifficulty; if(degreeOfDifficulty < 1 || degreeOfDifficulty > 1.67) { cout << "Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)" << endl; // display invalid goto IfError3; } //------Display the diver information with overall score cout << "\nDiver: " << diverName << ", City: " << diverCity << endl; averageNumbers = (total - (max + min)) / 3 * degreeOfDifficulty; overAllNumbers = (totalAverage / totalDivers); cout << "Overall score was " << setw(2) << averageNumbers << endl; cout << "\nDo you want to process another diver (Y/N)?: "; cin >> mainLoop; } while(mainLoopNo == 'n' || 'N') { //----------Summary of events cout << "\nNumber of divers participating: " << totalDivers << endl; overAllNumbers = (totalAverage / totalDivers); cout << "Average score of all divers: " << overAllNumbers << endl; break; } int key; cout << "\nPress any key"; cin >> key; }``````
Are you trying to make (totalAverage) the average of all the divers?
yes i wanted it to add all the diver if there's was more to process
i still can't figure out why it keep giving me 0 for Average score of all diver...can someone look through my code and if u can spot the error?
`void main()`

main should always return an int:

 ``123456`` ``````int main () { return 0; //if all is well } ``````

This doesn't do what you think:

 ``12`` ``````char mainLoop = 'y' || 'Y', mainLoopNo = 'n' || 'N';``````

You can only assign one value to a variable at a time.

This:
`while(mainLoop == 'y' || 'Y')`

should be this:

`while(mainLoop == 'y' || mainLoop == 'Y')`

However check this out for a much better way to do this:

 http://www.cplusplus.com/forum/beginner/84889/#msg455481

DO NOT USE GOTO:

Forget they even exist. Use loops or functions to achieve the logic you want.

Use a for loop, rather than a do loop when the end condition is a number.

Also make more use of functions, rather than having lots of code in main.

See how you go with all that, then we can see how things look.

HTH

an easy way to to do a do.. while loop is to use it like this....

#include <iostream>

using namespace std;

int main()

{
//declared variables

char again = 'y';

do
{

//write your code here, and after youre done ask if you want to try again.

cout << "Do you want to try again?";
cin >> again;

} while((again == 'Y') || (again == 'y'));

return 0;
}

If you have a look at the link I provided above, you might see how it is better than your example IMO.

Also, always use code tags - the <> button on the right, when posting code. It makes the code easier to read read because it preserves the formatting.

Hope all is well :)
Last edited on
@TheIdeasMan

thanks for the tips, i just started and the lab i had to do said i had to use a Do-While but in reality i heard a While loop is way better and no one really uses do-while statements...that's why i used a GOTO.

I understand and read somewhere that goto should never be used.

but i still dont know why this code won't work

 ``123456`` `````` { //----------Summary of events cout << "\nNumber of divers participating: " << totalDivers << endl; overAllNumbers = (totalAverage / totalDivers); cout << "Average score of all divers: " << overAllNumbers << endl;``````

Line 116: Should this be an if statement? The value of mainLoopNo isn't changed inside the loop, also it suffers from the same logic error mentioned above, making it always false. If you change that, and the program works, don't be tempted to leave the rest of the code as it is.

Have you made any changes to the code that I mentioned earlier? Your teacher will probably mark you down for using goto. All of your goto's could be fixed with while loops. If I was the teacher, I would give more marks for clear, elegant code. 50% even, so if it works, but is hard to read & un-organised - then you would get half marks.

Was a do loop specifically asked for, or was it just a suggestion? As I said earlier, a for loop would be better than your current do loop.

Lets see your new code, and we can go from there.
Topic archived. No new replies allowed.