hey im having trouble workin out the average it keeps showing 0 instead

#include<iostream>
using namespace std;

int main()
{
    int count;
    double grade, total, average;

    grade = 0;
    total = 0;
    count = 0;
    cout <<"\nTo stop entering grades. Type in the number";
    cout <<"\n 999.\n\n";

    total = total + grade;
          cout << "Enter a grade: ";
          cin >> grade;
          count++;

    while (grade != 999)

    do
{
    cout <<"\nEnter a grade: ";
    cin >> grade;
    if (grade < 0 || grade > 100 && grade !=999)
    {
    cout <<"\n Invalid grade has been entered"
         <<"\nPlease check the grade and re-enter";
}
else
break;    //break if a valid grade was entered
}
while(1); //this expression is always true

average = total / count;
cout << "\nThe average of the numbers is " << average << endl;
system("pause");
return 0;
}

Recommended Answers

All 4 Replies

#include<iostream>
using namespace std;

int main()
{
    int count;
    double grade, total, average;
    
    grade = 0;
    total = 0;
    count = 0;
    cout <<"\nTo stop entering grades. Type in the number";
    cout <<"\n 999.\n\n";
    
    total = total + grade;
          cout << "Enter a grade: ";
          cin >> grade;
          count++;
          
    while (grade != 999)
    
    do
{
    cout <<"\nEnter a grade: ";
    cin >> grade;
    if (grade < 0 || grade > 100 && grade !=999)
    {
    cout <<"\n Invalid grade has been entered"
         <<"\nPlease check the grade and re-enter";
}
else

     total = total + grade;
         count++;
break;    //break if a valid grade was entered

}
while(1); //this expression is always true

average = total / count;
cout << "\nThe average of the numbers is " << average << endl;
system("pause");
return 0;
}

Made the changes in red. Didn't check. Don't know if new bugs are introduced. You check.

it still doesnt calculate average the question is:
write a C++ program that continously represents a grade to be entered. if the grade is less than 0 or greater than 100, your program should print an appropriate message informing the user that an invalid grade has been entered; otherwise, the grade should be added to a total. when a grade of 999 is entered, the program should exit the repetition loop and coompute and display the average of the valid grades entered.

#include<iostream>
using namespace std;

int main()
{
    int count;
    double grade, total, average;
    
    grade = 0;
    total = 0;
    count = 0;
    cout <<"\nTo stop entering grades. Type in the number";
    cout <<"\n 999.\n\n";
          
    do
	{
	    cout <<"\nEnter a grade: ";
	    cin >> grade;
	    if (grade < 0 || grade > 100 && grade !=999)
	    {
			cout <<"\n Invalid grade has been entered"
	         <<"\nPlease check the grade and re-enter";
		}
		else
		{
			if ( grade != 999 )
			{
				total = total + grade;
				count++;
			}
			else
			{
				break;    //break if 999 was entered
			}
		}
	}
	while(1); //this expression is always true

	average = total / count;
	cout << "\nThe average of the numbers is " << average << endl;
	system("pause");
	return 0;
}

thanks heaps man ur a champ!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.