please help with homework

here is my code.... for some reason my program won't calculate the grade it just gives me an f

#include "stdafx.h" // Needed for Visual C++ Express 2010
#include <iostream>
using namespace std;

int main()
{

const int MAXNUMBEROFGRADES = 5;

float currentgrade, sumofgrades, average;
unsigned short int numberofvalidgrades, numberofinvalidgrades;
char lettergrade;


while (numberofvalidgrades < 5)
{
cout << "Enter a VALID grade (between 0 and 100): ";
cin >> currentgrade;
cin.ignore();

if (currentgrade < 0 || currentgrade > 100)
{
++numberofinvalidgrades;
continue;
}

++numberofvalidgrades;
if (numberofvalidgrades == MAXNUMBEROFGRADES)
break;



}
sumofgrades += currentgrade;

average = sumofgrades / numberofvalidgrades;

if (average >= 90.0)
{
lettergrade = 'A';
}
else if (( average >= 80.0) && (average < 90.0))
{
lettergrade = 'B';
}
else if ((average >= 70.0) && (average < 80.0))
{
lettergrade = 'C';
}
else if ((average >= 60.0) && (average < 70.0))
{
lettergrade = 'D';
}
else if (average < 60.0)
{
lettergrade = 'F';
}

cout << numberofvalidgrades << " were processed, your letter grade is: " << lettergrade << endl;
cin.ignore();

return 0;

}
Well, does your program assume the person is going to enter five grades? If not you will want to use a "for" loop for this it will cut down on a majority of the code needed. I'll give you a quick skeleton. Also your >100 statement means that bonus points would be considered invalid.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
  int nGrades;
  int grade;
  int avg;
   cout<<"Please enter in a grade no lower than 0'<<endl;
   cout<<"Enter in -1 to end the program if you have less than 5 grades"<<endl;
      for (nGrades=0;nGrades<=5;nGrades++)// Tells(Initial Value;The condition;and increment 
                                               or How much to increase the value per time for loop is repeated)
        {
             cin>>grade
              if (grade==-1) //your sentinal value to allow the person to calculate if they have less than 5 grades
              
              break;
        }
        calc avg
       if (avg ...)
         cout.... 
Last edited on
The problem is line 11 they are uninitialized then you try and use a while loop with that uninitialized value maybe set it equal to 0?
for some reason my program won't calculate the grade it just gives me an f

Your code always gives an f because you are not summing your grades correctly your sumofgrades += currentgrade; needs to be within the while loop.

side note: Microsoft visual studio 2010 is out of date upgrade to 2012
Last edited on
@sl227

First, please use code tags: Edit your post, select all the code, then press the <> button on the right (under format). This makes it look much nicer, formats it properly and provides line numbers.

This bit:

sumofgrades += currentgrade;

is outside the loop, so your loop just keeps overwriting the currentgrade variable, and sets the sum variable to whatever the last currentgrade was, hence the F grade.

HTH

Edit:

With the naming of variables, I like to use CamelCase: as in NumberOfValidGrades, NumberOfInvalidGrades ; making it slightly easier to tell the difference between the two in this case. You could abbreviate a little & get rid of the Of: NumValidGrades, NumInvalidGrades. Readability is an important thing when writing code.

Initialising variables to something on declaration is usually a good idea (as giblit pointed out). it will save you one day. Sometimes zero is not a good choice, so one could pick a value that shows invalidity, but doesn't give misleading answers.
Last edited on
Yeah I just figured his loop would start at 0. I think the while loop doesn't even run. By default it is probably like 34k and the loop only runs when it is less than 5 and increments by one each time I figured he only wanted it to run 5 times so starting at 0 and going up by 1 each time but it could always start at 3 and end at 5 but it is hard to tell with the uninitialized value.

1
2
3
4
5
6
unsigned short int numberofvalidgrades;

while (numberofvalidgrades < 5) //will never call the loop
{
//stuff
}


*Edit* I Think the op should learn to use proper indentations also.
1
2
3
4
5
6
7
8
int main()
{
short a( 3 );
if( a < 10 )
{
std::cout << a << std::endl;
}
}

^^ NOT the standard way to write and very hard to read.

1
2
3
4
5
6
7
8
9
int main()
{
    short a( 3 );

    if( a < 10 )
    {
        std::cout << a << std::endl;
    }
}

^^PROPER and much easier to read.
Last edited on
Topic archived. No new replies allowed.