Program a student uses the computer to figure out his average in this class. As long as he enters grades between 0 and 100, it computes the average. If he enters -1 for a grade, it stops computing and prints out the average.

I need help please someone help me i am new to C++.

What have you tried so far? Unless you show us your attempt, we'll assume that you didn't even try to think about it before running here for help.

#include <iostream.h>

int main()

{

    int     total,          
        gradeCounter,
        grade, 
        average;

    total = 0;
    gradeCounter = 1;

    do
        while (gradeCounter == -1)
        {
            cout <<" Enter grade:";
            cin>> grade;
            total = total + grade;
            gradeCounter = gradeCounter +1;
        }
        average = total/gradeCounter;
        cout << "Class average is " << average << endl;

        return 0;

}

Edited 3 Years Ago by happygeek: fixed formatting

error C2061: syntax error : identifier 'average'

not sure whats wrong with it. Am i approaching this correctly?

A do...while looks like this:

do {
   /* do stuff */
} while ( /* your condition */ );

And the semicolon at the end is important.

the thing is, i dont think i set up my program properly. when i read the description it says use -1 to end the calculations.

#include <iostream.h>

int main()

{

    int total,          
        gradeCounter,
        grade, 
        average; 

    total = 0;
    gradeCounter = 1;

    do  
    {
            cout <<" Enter grade:";
            cin>> grade;
            total = total + grade;
            gradeCounter = gradeCounter +1;

        average = total/gradeCounter;

        cout << "Class average is " << average << endl;


    } 
    while (gradeCounter != -1);


        return 0;

}

I tried but it's not calculating correctly and everytime i insert -1 it will not end my program and calculate the average instead it outputs a number =(.

Edited 3 Years Ago by happygeek: fixed formatting

#include<iostream.h>
int main()
{
int grade;
int totalpoints;
int numberofgrades = 0;

while(1)
{
cout<<"enter your grade (0 - 100)";
cin >> grade;
  if (grade = -1)
 {
   break;
 }
  else
  {
   totalpoints = totalpoints + grade;
   numberofgrades++;
  }
}
cout<<"This is your average grade: "<<(totalpoints / numberofgrades);
}

This is how i would go about doing it, all you need to do is use an infinite loop prompting the user for input, then say if the input = -1 break from the loop. After every time a grade is entered add it to the total and count how many grades have been entered, then when the loop is ended, calculate the average score. Im not too sure about using Integer as the type when you have to enter -1, I'm not too sure whether this is classed as an integer or not, but ask the people here, and they will help you sort it out

PS: Sorry i didnt use the same variable names as you as i just wrote it from my head on the spot, and i didnt test it either, so there may be errors

sorry, just noticed a slight mistake in syntax

if (grade == -1) //forgot the extra =

so i didn't need a do whole loop to finish program. My professor said i did so i was trying to figure it out. :( is there any tips you guys can give me. Thanks for the help by the way i really appreciate it.

bops i have a question in your code what does the (1) in your while loop do?

It's seldom the case that there is only one way to write a program. Your professor my require to use a do/while loop as a learning exercise, eventhough from a general programming standpoint there are other ways. Also, a do/while loop makes a lot of sense in this instance because you want to gaurantee that the body of the loop is done at least once. It would be a useful excercise for you to try writing a do/while loop, even if you don't "have to". Besides, personally, I think it is wise to avoid intentionally using infinite loops when possible, but that is a matter of personal preference, not programming correctness.

EDIT:

while(1)
{
}

is an infinite, neverending loop that will run until you turn off the computer or until you run out of memory or unless there is some way out of the written into the body of the loop, for example a break statement triggered by some input, or whatever.

#include <iostream.h>
int main()
{
int grade;
int totalpoints;
int numberofgrades = 0;

do
{
cout<<"enter your grade (0 - 100)";
cin >> grade;
  if (grade = -1)
 {
   break;
 }
  else
  {
   totalpoints = totalpoints + grade;
   numberofgrades++;
  }
}while(grade != -1)
cout<<"This is your average grade: "<<(totalpoints / numberofgrades);

return 0;
}  <---------- fatal error C1004: unexpected end of file found
Error executing cl.exe.

ok i've tried to revise the code to use a do while lopp and here is what i have come up with but for some reason, im not sure of what that error is but maybe you guys have an idea. It won't let me complie my program because it keeps pointing to that brace. Thanks again.

Edited 3 Years Ago by happygeek: fixed formatting

*Ahem*

A do...while looks like this:

do {
   /* do stuff */
} while ( /* your condition */ )[B];[/B]

And the semicolon at the end is important.

[Emphasis in this reply.]

This is assignment:

if (grade = -1)

This is comparison:

if (grade == -1)

[Emphasis in original.]

#include <iostream.h>
int main()
{
int grade;
int totalpoints;
int numberofgrades = 0;

do
{
cout<<"Enter your grade from (0-100) press -1 for average\n";
cin >> grade;
if (grade == -1)
{
break;
}
else
{
totalpoints = totalpoints + grade;
numberofgrades++;
}

}while(grade != -1);
cout<<"This is your average grade:"<<(totalpoints / numberofgrades);

return 0;

}

Still not workng properly it complies but when i enter the grades for example: 90 , 90 ,80 , 80 , 54, -1 to calculate average. It will outpit a -121334 #. What am i doing wrong please help. Thanks in advance.

Initialize totalpoints with 0

int grade;
int totalpoints=0;
int numberofgrades = 0;

When using an infinite loop like the while(1) you need some way out, like a break statement. However, in the do/while, you don't need a break statement and you don't need an else statement. You can just say, in effect, if grade not equal to minus one then do calculation, else when grade is minus stop do/while loop. In effect, the conditional of the while() can act as the else.

This article has been dead for over six months. Start a new discussion instead.