I got some unexpected value on the screen

I made a grade report program and I face some problems.

1. total average is half of what is supposed to be.
2. I got some values of -85993460 -858993460
below is the example of what I get after I choose to see all grades

id name kor eng math ave
1 kevin 70 80 90 70
-85993460 -858993460

total average : 35.00



Here is my code, thanks guys

Thanks

class Student{
public:
int idnum;
string name;
int kor, eng, math;
float ave;

};

int main(){
const int max = 100;
Student s[max];

float total_ave = 0.0f;
int count = 0;

while (1){
cout << "\n ------menu------\n";
cout << "1. add student ";
cout << "2. show all grades ";
cout << "Q. exit \n";
cout << "select number";

char select;
cin >> select;

switch (select){
case '1':{
if (max == count){
cout << "\n overflow\n";
break;
}
Student& std = s[count];
std.idnum = ++count;
cout << "enter name and grades of kor, eng, math: \n ";
cin >> std.name >> std.kor >> std.eng >> std.math;

std.ave = float(std.kor + std.eng + std.math) / 3.0f;

const int curstd = count + 1;
const int prevstd = count;

total_ave = (total_ave * prevstd / curstd) + (std.ave / curstd);

++count;


cout << "\n entered correctly";

break;

}

case '2':{
cout.precision(2);
cout << fixed;

cout << "\n <see all grades>";
cout << "\n id name kor eng math ave \n";

for (int i = 0; i < count; i++){

const Student& std = s[i];
cout << setw(3) << std.idnum <<setw(7) << std.name;
cout << setw(5) << std.kor << setw(5) << std.eng;
cout << setw(5) << std.math << setw(7)<< std.ave << "\n";
}

cout << "\n total average: " << total_ave <<
"\n[/i]";
[/u]
break;
}
Last edited on
1
2
3
const int curstd = count + 1;
const int prevstd = count;
total_ave = (total_ave * prevstd / curstd) + (std.ave / curstd);

The value of curstd is 2. Hence total_ave results in half of what it should be. Also, why do you declare curstd and prevstd as const when you are trying to assign their values to a variable count.
Thank you abhishekm71
I changed curstd and prevstd and erase ++count then I got expected value.
Also, why do you declare curstd and prevstd as const when you are trying to assign their values to a variable count.

That's a perfectly legitimate thing to do. It specifies that, after assigning an initial value, the value of the variable can never change. It doesn't matter whether that initial value is derived from another non-const variable.
That's a perfectly legitimate thing to do. It specifies that, after assigning an initial value, the value of the variable can never change. It doesn't matter whether that initial value is derived from another non-const variable.

I am aware of that. My question pertained to the code logic. I wanted to know whether there was any specific need to declare those variables in such a way.

EDIT:
However, I am not clear how const variables (pun not intended) would behave when declared inside a loop, such as in this case. Would the code create a new const element at each iteration? Or would they behave something like static variables?
Last edited on
However, I am not clear how const variables (pun not intended) would behave when declared inside a loop, such as in this case. Would the code create a new const element at each iteration? Or would they behave something like static variables?

The former. Making a variable const doesn't change its scoping in any way. It's still a local variable, whose scope is the block contained within the for loop. At the end of each iteration, it will fall out of scope and be destroyed, like any other local variable.

const is just a contract that the value of the variable can't be changed after initialisation. And that's exactly what's happening in this case. I don't see anything remarkable about its use here.
ok thanks!
You're welcome :)
Topic archived. No new replies allowed.