This program ask user which arithmetic operation they want to play with then generates a random equation and lets the user answer it. When the user stop playing the game the program calculates the number of correct and answers and wrong answers.
I need some help because the program crashes when doing divisions.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
//include header file
#include "lab_17_head.h"
using namespace std;
int main( )
{
//variable declaration
int number1, number2; //vars to store int values
double intResult, doubleResult, result; //vars to store the result
char action; //var for store the operator
char flag; //var to stor the choice
int plusWins, plusLosses, plusTotal, //plus stats for winning, losing, and total games played
minuWins, minuLosses, minuTotal, //minus stats for winning, losing, and total games played
multWins, multLosses, multTotal, //multiplication stats for winning, losing, and total games played
diviWins, diviLosses, diviTotal; //division stats for winning, losing, and total games played
double answer; //var for answer from the user
//initializations
plusWins = plusLosses = plusTotal = 0;
minuWins = minuLosses = minuTotal = 0;
multWins = multLosses = multTotal = 0;
diviWins = diviLosses = diviTotal = 0;
//show welcome message
welcome();
//enter your choice
getChoice(flag);
//start loop to play the game
while(flag=='y' || flag=='Y')
{
//get operator
getOperator(action);
//four cases for action
switch(action)
{
case '+': //do addition
//get two random numbers of two digits
getTwoNumbers(number1, number2);
//add
intResult = add(number1, number2);
//getAnsers from the user
getAnswer(number1, number2, action, answer);
//win or loss
addGame(intResult, static_cast<int>(answer), plusWins, plusLosses, plusTotal);
break;
case '-': //do substraction
//get two random numbers of two digits
getTwoNumbers(number1, number2);
//add
intResult = minus(number1, number2);
//getAnsers from the user
getAnswer(number1, number2, action, answer);
//win or loss
minuGame(intResult, static_cast<int>(answer), minuWins, minuLosses, minuTotal);
break;
case '*': //do multiplication
/***************************************************
This part is for you to complete
***************************************************/
//get two random numbers of two digits
getTwoNumbers(number1, number2);
//multiply
intResult = multiply(number1, number2);
//getAnsers from the user
getAnswer(number1, number2, action, answer);
//win or loss
multiGame(intResult, static_cast<int>(answer), multWins, multLosses, multTotal);
break;
case '/': //do division
/***************************************************
This part is for you to complete
***************************************************/
//get two random numbers of two digits
getTwoNumbers(number1, number2);
//add
intResult = divide(number1, number2);
//getAnsers from the user
getAnswer(number1, number2, action, answer);
//win or loss
diviGame(result, static_cast<int>(answer), diviWins, diviLosses, diviTotal);
break;
default:
cout<<action<<" is not a valid operator ." <<endl;
}
//get choice
getChoice(flag);
}
//show stats
statsShow(plusWins, plusLosses, plusTotal, minuWins, minuLosses, minuTotal,
multWins, multLosses, multTotal, diviWins, diviLosses, diviTotal);
//byebye
bye();
//well done and exit
return 0;
}
This is the header used
#include <iostream>
#include <cstring>
using namespace std;
#ifndef LAB_17_HEAD_H
#define LAB_17_HEAD_H
//show welcome message
void welcome()
{
cout<<" Welcome to my math world and enjoy some training ...... "<<endl;
return; //no value is returned here
}
//get the choice
void getChoice(char & ch) //use reference paramter to receive the choice
{
cout<<"\n Would you like to play the game? (y/n) => ";
cin>>ch; //get the choice
return;
}
//get operator
void getOperator(char & action) //use reference paramter to receive the action
{
cout<<"\n Which operation do you like to play? (+, -, *, /) => ";
cin>>action;
return; //get the choice
}
//get two random numbers of two digits
void getTwoNumbers(int & number1, int & number2) //use reference paramter to receive the action
{
unsigned seed; //local variable for random seed
//get a random seed
cout<<"\n Give me an integer for random =>";
cin>>seed;
//set the random seed
srand(seed);
//get two random numbers of single digit
number1 = rand()%10;
number2 = rand()%10;
return;
}
//do addition
int add(const int number1, const int number2) //const value parameters
{
return number1 + number2; //return the sum
}
//do substraction
int minus(const int number1, const int number2) //const value parameters
{
return number1 - number2; //return the substraction
}
//do addition
int multiply(const int number1, const int number2)//const value parameters
{
return number1 * number2; //return the multiplication
}
//do addition
double divide(const int number1, const int number2)//const value parameters
{
return static_cast<double>(number1) / static_cast<double>(number2);
//return the division
}
//getAnsers from the user
void getAnswer(const int number1, const int number2, const char action,
double & answer)
{
cout<<endl<<number1<< " " <<action <<" " <<number2<< " = ? ";
cin>>answer;
}
//addition win or loss
void addGame(const int intResult, const int answer,
int & plusWins, int & plusLosses, int & plusTotal)
{
if (answer == intResult) //decide winning
plusWins++;
else //or loss
plusLosses++;
plusTotal++; //counting the games
}
//substrcation win or loss
void minuGame(const int intResult, const int answer,
int & minuWins, int & minuLosses, int & minuTotal)
{
/*********************************************************
This part is for you to complete
*********************************************************/
if (answer == intResult) //decide winning
minuWins++;
else //or loss
minuLosses++;
minuTotal++;
}
//multiplication win or loss
void multiGame(const int intResult, const int answer,
int & multWins, int & multLosses, int & multTotal)
{
/*********************************************************
This part is for you to complete
*********************************************************/
if (answer == intResult) //decide winning
multWins++;
else //or loss
multLosses++;
multTotal++;
}
//division win or loss
void diviGame(const double result, const double answer,
int & diviWins, int & diviLosses, int & diviTotal)
{
/********************************************************
This part is for you to complete
********************************************************/
if (answer == result) //decide winning
diviWins++;
else //or loss
diviLosses++;
diviTotal++;
}
//show stats
void statsShow(const int plusWins, const int plusLosses, const int plusTotal,
const int minuWins, const int minuLosses, const int minuTotal,
const int multWins, const int multLosses, const int multTotal,
const int diviWins, const int diviLosses, const int diviTotal)
{
double plusScore, //local variable for scores
minuScore,
multScore,
diviScore;
//compute the scores
plusScore = plusTotal==0 ? 0: static_cast<double>(plusWins)/static_cast<double>(plusTotal);
plusScore *= 100;
minuScore = minuTotal==0? 0: static_cast<double>(minuWins)/static_cast<double>(minuTotal);
minuScore *= 100;
multScore = multTotal==0? 0: static_cast<double>(multWins)/static_cast<double>(multTotal);
multScore *= 100;
diviScore = diviTotal==0? 0: static_cast<double>(diviWins)/static_cast<double>(diviTotal);
diviScore *= 100;
cout<<endl;
//for addition
cout<<" You played "<<plusTotal<<" additions."<<endl
<<" You won " <<plusWins <<" many times." <<endl
<<" You lost "<<plusLosses<<" many times." <<endl
<<" Your addition score is " <<setprecision(2)<<fixed<<showpoint
<< plusScore
<<endl<<endl;
//for substraction
cout<<" You played "<<minuTotal<<" substractions."<<endl
<<" You won " <<minuWins <<" many times." <<endl
<<" You lost "<<minuLosses<<" many times." <<endl
<<" Your substration score is " <<setprecision(2)<<fixed<<showpoint
<< minuScore
<<endl<<endl;
//for multiplication
cout<<" You played "<<multTotal<<" multiplications."<<endl
<<" You won " <<multWins <<" many times." <<endl
<<" You lost "<<multLosses<<" many times." <<endl
<<" Your multiplication score is " <<setprecision(2)<<fixed<<showpoint
<< multScore
<<endl<<endl;
//for division
cout<<" You played "<<diviTotal<<" divisions."<<endl
<<" You won " <<diviWins <<" many times." <<endl
<<" You lost "<<diviLosses<<" many times." <<endl
<<" Your division score is " <<setprecision(2)<<fixed<<showpoint
<< diviScore
<<endl<<endl;
}
//byebye
void bye()
{
cout<<"Hope you enjoyed playing this game. Byebye." <<endl<<endl;
return;
}
#endif