With this program, I am trying to make a program that has the user input 10 grades to be averaged. That is the easy part. I got that working, and now I am trying to make it so if the user inputs "-1", then that assignment has not been completed, and therefore will not factor into the average. I can't figure out how to do this. With what I have so far, I keep getting an error saying:
"hw15.cpp:46: error: name lookup of âiâ changed for ISO âforâ scoping
hw15.cpp:46: note: (if you use â-fpermissiveâ G++ will accept your code)" If anyone knows what that means, can they be of some assistance? Here is my code so far.
See, the reason i did  is because when i ran the program with grades it asked for 11 grades instead of 10. So i changed it to  and it works. I'm just trying to make it so -1 is counted as an ungraded assignment, not counting toward the average.
If you don't mind having segfaults or otherwise undefined behavior, then yes, I suppose it does.
it asked for 11 grades instead of 10
That's because you need to re-evaluate your for loops. Use < instead of <= to avoid that. Handling an array properly looks like this:
1 2 3 4
constint amount = 32;
int things[amount]; // has range 0-31 (32 indices)
for(int i = 0; i < amount; ++i) // loops i from 0-31
things[i] = 0;
To ignore a score:
Check if the score is -1. If it's not, add it to the sum. If it is, don't. Keep track of how many scores you skip, because to get an average, you need to divide by the number of items summed.
There are some major problems with what you have here, which is what MikeyBoy was trying to point out, but you didn't see what he's talking about so I'll just say it.
Your loop will only run once, because of line 18. Take both lines 17 and 18 out of the loop.
Now let's go through what your loop does (once you move lines 17 and 18).
Check if the grade is -1. If it isn't, add it to the sum. If it is, add -1 to "score." Then return -1 from the function.
I'm not sure what "score" is or why you are using return. return exits from the function immediately.
Might I suggest instead of using whatever "score" is, use a variable that counts the number of grades that you have skipped (perhaps int skipped = 0;?). Then if the grade is -1, increment "skipped" by 1. After the for loop ends, divide the sum by (num - skipped). Note that the parenthesis are important, or else the order of operations will be incorrect, and your average will not be right.
Okay, so the program itself works wonderfully, EXCEPT when the user inputs -1 for every number. I understand what happens when that happens, because after the loop is run, the formula avg = sum / (num - skipped) is then put into play. When -1 is put into every grade, the sum = 0 and skipped = 10 so that means that the formula that is run is avg = 0 / (10-10) . Since the final answer for that is undefined, how could I tell the program that if the answer is undefined then to have the output be
"Average Grade: ---%"
Here's the average grades function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
float averageGrades(float grades, int num)
int sum = 0;
int avg = 0;
int skipped = 0;
for(int i = 0; i < num; i++)
if (grades[i] == -1)
skipped += 1;
sum += grades[i];
avg = sum / (num - skipped);
Before you carry out the calculation on line 14 of averageGrades, make sure that num != skipped. If that's not the case, don't do the calculation and return a number that signifies that there is no average (maybe a negative number?).
Then handle the return value of averageGrades accordingly in main. You've already done this part, though.
int getAverage(int grades, int num)
int sum = 0;
for (int i = 0; i < 10; i++)
if (grades[i] >= 0)
sum += grades[i];
if (sum <= 0)
cout << "Average Grade: ---%\n";
cout << "Average Grade: " << sum / num << "%" << endl;