### 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()
{
double average;
double total;

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++)
{

cout << "Enter quiz grade ";

cout << "Enter quiz grade ";

cout << "Enter quiz grade ";

cout << "Enter quiz grade ";
cout << endl;
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
www.cplusplus.com/articles/z13hAqkS/

Also:

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

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:

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.