post the rest of that function becase what you posted is not enough to tell us anything about the problem you have.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
try this:
if (grade[i]<0)
{
cout << "This is not a correct grade";
break; // exit the loop
}
Also -- you need a counter to count the number of grades entered because there may be fewer than MAXGRADES. Then use that counter in the code that follows.
>>if (MAXGRADES>0)
That is an impossible comparison because MAXGRADES will never be anything other than what it is initialized to be at the beginning of the program. Instead of MAXGRADES you need to use the counter that I mentioned above.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>> Is that why I need to change the counter?
yes, because the elements of the array that you did NOT enter contain random values. You should probably initialize the array elements to all 0 at the time it is declared like this
int grade[MAXGRADES] = {0};
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
not necessary, just declare another int named count , initilize it to 0, then increment it in that loop maybe something like this:
for (i=0; i < MAXGRADES; i++, count++)
Then replace for (i=0; i<MAXGRADES; i++)
with this for (i=0; i<count; i++)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
not necessary, just declare another int named count , initilize it to 0, then increment it in that loop maybe something like this:
for (i=0; i < MAXGRADES; i++, count++)
Then replace for (i=0; i<MAXGRADES; i++)
with this for (i=0; i<count; i++)
Or to make it simple, specify the first loop as for (count = 0; count < MAXGRADES; count++)
This will automatically count the grades entered, and you won't needi at all.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944