Help with <cmath>

Hello,
This is my code. I am trying to average 4 grades, however every time I run the program it does not produce a numeric value. I cant figure out what I am doing wrong. Any pointers would be greatly appreciated. btw. I have the <fstream> because when the program is finished I will put it in a file. Thanks

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>
#include<cstdlib>
using namespace std;

int main()
{
int grade1;
grade1 = 0;
int grade2;
grade2 = 0;
int grade3;
grade3 = 0;
int grade4;
grade4 = 0;
double average;
double total;
total = (grade1 + grade2 + grade3 + grade4) / 4;

int num;
num = 0 || 1;
int student_Id;
student_Id = 0;



//Enter student Id
cout << "Enter the student id\n";
cin >> student_Id;

for (int x = 0; x < 3; x++)
{


//Enter quiz grade
cout << "Enter quiz grade ";
cin >> grade1;

cout << "Enter quiz grade ";
cin >> grade2;

cout << "Enter quiz grade ";
cin >> grade3;

cout << "Enter quiz grade ";
cin >> grade4;
cout << endl;
total = (grade1 + grade2 + grade3 + grade4) / 4;
cout << "Student average is <<total<<endl";


cout << "enter 0 for no more students to enter. Enter 1 for more students.\n";
cin >> num;
cout << endl;
if (num == 1)
continue;
if (num == 0)
break;
}



system("pause");


}
Last edited on
Um... I don't think that total does produce a numeric value, but you're using the quotations wrong in the cout statement.cout << "Student average is <<total<<endl"; //The quotations on this line should be corrected

You have an unused variable and that is the variable average.

Don't forget return 0;

Do you need to consider if the user enters a value other than a 0 or 1 at the end?

I'm genuinely curious, what does this mean? num = 0 || 1; //I'm not trying to sound sarcatic...

If you're going to store multiple student ID's and averages then it might be wise to use a vector or array.

That's all for now. Hopefully, I helped a bit. Good luck!
Last edited on
Please always use code tags:
www.cplusplus.com/articles/z13hAqkS/

Also:

total = (grade1 + grade2 + grade3 + grade4) / 4;

That is doing integer division, probably not what you want.

double total = (grade1 + grade2 + grade3 + grade4) / 4.0;

The 4.0 forces the promotion to double. I always put digits before and after the decimal place for doubles, it prevents these type of errors. Remove the first time you have that statement: you don't have the data yet.

This:
1
2
int grade1;
grade1 = 0;


Is more efficiently written as:
int grade1 = 0; // declaration and assignment in one statement.

Generally one can wait until there is a sensible value before doing declaration and assignment.

verdantgale wrote:
Don't forget return 0;


that isn't actually required in C++, but there is no harm in having it anyway.

Using std::vector is a good idea, learn C++ , not C.

The continue statement is used in a loop, not an if statement. It means start the loop again, so it makes no sense in an if statement.

With the cout , always read the documentation about things you are not sure about.

Good Luck !!
Thank you both for the reply. All the information was very helpful and I was able to make the corrections and get the code running. To Verantgal, that num=0||1 was my very bad attempt at making a variable...Yeah that didn't work out too well lol..
Topic archived. No new replies allowed.