Hello. I have two problems. One is that I cannot get my numbers to display properly. If you run the program with certain values that should clearly average and show 90.00, it is instead displaying "89". I don't know why I can't get it to display correctly. I don't know how to use setprecision(2) properly, or something.
My other question is: how do you compare words?
I must have misunderstood something in lecture. I tried to declare
char choicemore;
and then say if (choicemore=="y")
but it didn't work. How would I do that properly? Here's my code if you wanna see it for specific errors.
#include<iostream>
#include<cmath>
usingnamespace std;
int main()
{
float choiceprogram;
float choicequiz;
float choicetest;
float programavg = 0;
float programscores = 0;
float programmax;
float quizavg = 0;
float quizscores = 0;
float quizmax;
float testavg = 0;
float testscores = 0;
float testmax;
int choicemore;
float examavg;
float overallavg;
// This is the main loop of the program, that executes
// one time guaranteed, and continues when the user
// tells it 'y' after going through the loop once completely
do
{
cout << "Enter the maximum possible program points so far: ";
cin >> programmax;
cout << endl;
// This is the loop that accepts program scores
while (choiceprogram>-1)
{
cout << "Enter a program score (-1 to quit): ";
cin >> choiceprogram;
programscores+=choiceprogram;
}
cout << endl << "Enter the maximum possible quiz points so far: ";
cin >> quizmax;
cout << endl;
// This is the loop that accepts quiz scores
while (choicequiz>-1)
{
cout << "Enter a quiz score (-1 to quit): ";
cin >> choicequiz;
quizscores+=choicequiz;
}
cout << endl << "Enter the maximum possible test points so far: ";
cin >> testmax;
cout << endl;
// This loop is to verify that there are more than 0 test points possible
if (testmax!=0)
{
// This loop is to get test scores
while (choicetest>-1)
{
cout << "Enter a test score (-1 to quit): ";
cin >> choicetest;
testscores+=choicetest;
}
cout << endl;
}
programavg = programscores / programmax;
quizavg = quizscores / quizmax;
programavg*=100;
quizavg*=100;
cout << "Program average is: " << programavg;
cout << endl << "Quiz average is " << quizavg;
// This loop is to display the proper message based on tests entered or not
if (testmax==0)
{
cout << endl << "No tests entered so no test average calculated";
}
else
{
testavg = testscores / testmax;
testavg*=100;
cout << endl << "Test average is: " << testavg;
}
// The following loop is to calculate the exam average
if (testmax==0)
{
examavg = quizavg;
cout << endl << "Exam average is: " << examavg;
}
else
{
examavg = ((((testavg * 3) + quizavg)) / 4);
cout << endl << "Exam average is: " << examavg;
}
// This loop is for displaying the Overall average
if (testmax==0)
{
overallavg = ((.6*quizavg) + (.4*programavg));
cout << endl << "Overall average is: " << overallavg;
}
else
{
overallavg = ((.4*programavg) + (.15*quizavg) + (.45*testavg));
cout << endl << "Overall average is: " << overallavg;
}
// This next loop is the warning message for low test scores
if (examavg<55)
{
cout << endl << endl << "*** Your exam average is below 55%! ***" << endl;
}
else
{
cout << endl;
}
cout << endl << "Another (1/0)? ";
cin >> choicemore;
cout << endl;
choiceprogram = 0;
choicequiz = 0;
choicetest = 0;
overallavg = 0;
examavg = 0;
testavg = 0;
programscores = 0;
quizscores = 0;
testscores = 0;
programavg = 0;
quizavg = 0 ;
testavg = 0;
}
while (choicemore>0);
return 0;
}
Your first problem is floating point roundoff error because values like 0.4 and 0.6 do not have exact binary representations. The wrong way to solve the problem (but will work) is to use doubles instead of floats. The right way to solve the problem is to use integer calculations throughout and convert to floating point only when about to display the result (although even then it isn't strictly necessary to convert to floating point).
Your second problem is that
"Y" (double quotes) is a string whose type in C/C++ is char*. 'Y' (single quotes) is a character whose type in C/C++ is char.
To compare characters you can use ==, ie if( answer == 'y' )
To compare strings you must use strcmp.