Logical Error With c++ Program

I've made a math quiz with three different difficulty levels with addition, subtraction, multiplication, and division. I appear to be having a logical error with this program. At the end of my program I want it to tell the user how many they got correct (total score), their percent correct, and how many they got wrong. When I run the program using the g++ command it outputs the following:
Your total score is...0
Your percent correct is...nan
Your number wrong is...0

Any advise would be great on what this logical error could possible be. Thanks!

#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

void addition (int a, int b, double questioncount,int level, double rightcount, double wrongcount, int guess, int answer, int easy, int medium, int hard )
{
while(questioncount != 10){
questioncount ++;
if (level == easy) a =rand() %10, b =rand() %10;
else if (level == medium) a =rand() % 15, b =rand() % 15 ;
else if (level == hard) a =rand() % 20, b =rand() %20 ;
cout<< "What is " << a << " + " << b << "?" << endl;
answer = a+b;
cin>> guess;
if (guess == answer){
rightcount ++;
cout << "Correct" << endl;
}
if (guess != answer){
wrongcount ++;
cout << "Wrong" << endl;
}
}
}
void subtraction (int a, int b, double questioncount,int level, double rightcount, double wrongcount, int guess, int answer, int easy, int medium, int hard)
{
while(questioncount != 10){
questioncount ++;
if (level == easy) a =rand() %10, b =rand() %10;
else if (level == medium) a =rand() % 15, b =rand() % 15 ;
else if (level == hard) a =rand() % 20, b =rand() %20 ;
answer = a+b;
cout<< "What is " << answer << " - " << b << "?" << endl;
cin>> guess;
if (guess == a){
rightcount ++;
cout << "Correct" << endl;
}
if (guess != a){
wrongcount ++;
cout << "Wrong" << endl;
}
}
}
void multiplcation (int a, int b, double questioncount,int level, double rightcount, double wrongcount, int guess, int answer,int easy, int medium, int hard)
{
while(questioncount != 10){
questioncount ++;
if (level == easy) a =rand() %9, b =rand() %9;
else if (level == medium) a =rand() % 12, b =rand() % 12 ;
else if (level == hard) a =rand() % 15, b =rand() %15 ;
cout<< "What is " << a << " X " << b << "?" << endl;
answer = a*b;
cin>> guess;
if (guess == answer){
rightcount ++;
cout << "Correct" << endl;
}
if (guess != answer){
wrongcount ++;
cout << "Wrong" << endl;
}
}
}

void division (int a, int b, double questioncount,int level, double rightcount, double wrongcount, int guess, int answer, int easy, int medium, int hard)
{
while(questioncount != 10){
questioncount ++;
if (level == easy) a =(rand() %8)+1,b= rand() %9;
else if (level == medium) a =(rand() % 11)+1, b =rand() % 12 ;
else if (level == hard) a =(rand() % 14)+1, b =rand() %15 ;
answer = a*b;
cout<< "What is " << answer << " / " << a << "?" << endl;
cin>> guess;
if (guess == b){
rightcount ++;
cout << "Correct" << endl;
}
if (guess != b){
wrongcount ++;
cout << "Wrong" << endl;
}
}
}
int main(){
int level;
int easy= 1 ;
int medium= 2;
int hard= 3;
int type;
int add = 1;
int sub = 2;
int mult = 3;
int div = 4;
double questioncount = 0;
double wrongcount = 0;
double rightcount = 0;
int answer;
int guess;
int a;
int b;
cout<<"This is a math quiz with ten questions."<< endl;
cout<<"At the end you will find out how many you got right,"<< endl;
cout<<"and how many you got wrong." << endl;
cout<<"What would you like to do?"<< endl;
cout<<"Type 1 for addition, 2 for subtratction, 3 for multiplcation, 4 for division."<<endl;
cin >> type;
cout<< "What level would you like?" << endl;
cout<< "Type 1 for easy, 2 for medium, or 3 for hard." <<endl;
cin >> level;

// Srand (time(NULL));
if (type == add){
addition (a,b,questioncount,level,guess,answer,rightcount,wrongcount,easy,medium,hard);
}
if (type == sub){
subtraction (a,b,questioncount,level,guess,answer,rightcount,wrongcount,easy,medium,hard);
}
if(type == mult) {
multiplcation (a,b,questioncount,level,guess,answer,rightcount,wrongcount,easy,medium,hard);
}
if (type == div) {
division (a,b,questioncount,level,guess,answer,rightcount,wrongcount,easy,medium,hard);
}


ofstream of("score.txt");
of << (double) rightcount << endl;
of << ((double) rightcount/(double) questioncount)*100 << endl;
of << (double) wrongcount << endl;
of.close();

ifstream ifile("score.txt");
string s;
ifile >> s;
cout << "Your total score is..." << s << endl;
ifile >> s;
cout << "Your percent correct is..." << s << endl;
ifile >> s;
cout << "Your number wrong is..." << s << endl;
ifile.close();

return 0;
}

try using the code tags to make it easier for people to read.

Either way, One thing I notice you doing is when you call a function like addition for example you call it from your main function like this to pass your variables over to the function:

1
2
if (type == add){
addition (a,b,questioncount,level,guess,answer,rightcount,wrongcount,easy,medium,hard);}


but the actual function declaration looks like this.

void addition (int a, int b, double questioncount,int level, double rightcount, double wrongcount, int guess, int answer, int easy, int medium, int hard ){//Code Here}

I'm not sure if your problem lies there but you may try verifying that all your information is being passed over correctly.

Something to help you see the problem might be this:

If you had a function call like this addition (a,b,c)

and the function declaration was like this void addition (int X, int Y, int Z){}

X would hold the value of a, Y would hold the value of b, and Z would hold the value of c.

So in your code when you are passing the information up the data types aren't matching up because you are trying to put a double into an integer. I guess I just talked myself through your problem lol.

Make sure your data types match up.
Last edited on
Topic archived. No new replies allowed.