Issue with my program

Hey,

I've been trying to build a program that asks for

*number of students
*grade for each student
*calculates total grades
*gives min/max grade
*calculates the average

note that the grades can be in decimal form (EG. 13.6 , 17.4 etc....)

the program below only accepts integers and whenever I enter a decimal number it outputs the below weirdly.... however I have declared "float"

for example:

Give number of students: 10
Give marks of student 1:5
Give marks of student 2:6
Give marks of student 3:2.5
Give marks of student 4:Give marks of student 5:Give marks of student 6:Give marks of student 7:Give marks of student 8:Give marks of student 9:Give marks of student 10:Total: 13
Average: 1.3
Max: 6 Min: 0


Whenever I use integers ONLY,,, the program works perfectly,
when I use decimal numbers it does that to me



**************************
My Code
**************************

# include <iostream>
using namespace std;

int main(){

int count, numStudents;
float min, max, sum, average, marks;

sum = 0.0;
average = 0.0;
count = 1;

cout << "Give number of students: ";
cin >> numStudents;

while(count <= numStudents) {

cout << "Give marks of student " << count << ":" << endl;
cin >> marks;
sum = sum + marks;

if(count == 1){
//Set min/max values as the marks value
min = marks;
max = marks;
}
else{

min = (min > marks) ? marks:min;
max = (max < marks) ? marks:max;
}

count = count + 1;
}
average = sum / (count - 1);

cout << "Total: " << sum << endl;
cout << "Average: " << average << endl;
cout << " Max: " << max << " " << "Min: " << min << endl;

return 0;
}





**********************************************

can anyone help please???
Last edited on
closed account (48T7M4Gy)
Looks OK to me. Only compiler message you should address is a warning min and max possibly un-initialized when used in the else branch. Integer division of count may be a problem so divide by (float)count.
average = sum / ((float)count - 1);

Give number of students: 4
Give marks of student 1:
22.1
Give marks of student 2:
23.4
Give marks of student 3:
22.3
Give marks of student 4:
22.4
Total: 90.2
Average: 22.55
 Max: 23.4 Min: 22.1
Program ended with exit code: 0
Last edited on
I don't think you have compiled your new version - it actually runs fine.

If I change marks back to an int then it replicates your described behaviour when inputting marks with decimals.

So, I think you are accidentally trying to run with the old executable code.
Last edited on
thank you very much ,, appreciate your help

@kemort, @lastchance:

I noticed that when running the program it gives a message that there will be errors, so I created another source file with the same code and executed the file, so it worked perfectly.

@kemort:
I don't think count should be declared as float because average is already declared as float
closed account (48T7M4Gy)
"I don't think count should be declared as float because average is already declared as float "

It's only a small point but (float) casts count as a float, count is already declared as an integer earlier in the program. As I said, I did it to avoid possible truncation errors in the answer. But on reflection it is unnecessary. :)

http://www.cplusplus.com/doc/tutorial/typecasting/
Topic archived. No new replies allowed.