Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include "classFraction.cpp"
using namespace std;
void multFraction();
void addFraction();
void subFraction();
void divFraction();
int correct=0,wrong=0;
int main()
{
char choice;
int numOfQuestions=0;
do
{
cout<<"What type of exercise would you like to see?"<<endl;
cout<<"*************************************"<<endl;
cout<<"What type of exercise would you like to see?"<<endl;
cout<<"+, -, * , or /"<<endl<<endl;
cout<<"*************************************"<<endl;
cin>>choice;
switch(choice)
{
case '*':
multFraction();
numOfQuestions++; break;
case '+':
addFraction();
numOfQuestions++; break;
case '-':
subFraction();
numOfQuestions++; break;
case '/':
divFraction();
numOfQuestions++; break;
default:cout<<"Invalid choice";
}
cout<<"\n\nTotal Number of Questions asked: "<<numOfQuestions<<endl;
cout<<"Corect Answers: "<<correct<<endl;
cout<<"Wrong Answers: "<<wrong<<endl;
cout<<"\n Do you want to continue? (Y or N): ";
cin>>choice;
}while(toupper(choice)=='Y');
return 0;
}
void multFraction()
srand(time(0));
Fraction f1,f2,f3;//getting error
f1.set(rand()%15+1,rand()%15+1);
f2.set(rand()%15+1,rand()%15+1);
f1.display();
cout<<" * ";
f2.display();
cout<<" = "<<endl;
int n,d;
cout<<"Enter your answer: ";
cin>>n;
getchar();
cin>>d;
f3.set(n,d);
Fraction corrAns=f1*f2;
if(f3==corrAns)
{
correct++;
cout<<"Your answer is correct"<<endl;
}
else
{
wrong++;
cout<<"Your answer is not correct"<<endl;
cout<<"correct answer is: ";
corrAns.reduce();
corrAns.display();
}
}
void addFraction()
{
srand(time(0));
Fraction f1,f2,f3;
f1.set(rand()%15+1,rand()%15+1);
f2.set(rand()%15+1,rand()%15+1);
f1.display();
cout<<" + ";
f2.display();
cout<<" = "<<endl;
int n,d;
cout<<"Enter your answer: ";
cin>>n;
getchar();
cin>>d;
f3.set(n,d);
Fraction corrAns=f1+f2;
if(f3==corrAns)
{
correct++;
cout<<"Your answer is correct"<<endl;
}
else
{
wrong++;
cout<<"Your answer is not correct"<<endl;
cout<<"correct answer is: ";
corrAns.reduce();
corrAns.display();
}
}
void subFraction()
{
srand(time(0));
Fraction f1,f2,f3;
f1.set(rand()%15+1,rand()%15+1);
f2.set(rand()%15+1,rand()%15+1);
f1.display();
cout<<" - ";
f2.display();
cout<<" = "<<endl;
int n,d;
cout<<"Enter your answer: ";
cin>>n;
getchar();
cin>>d;
f3.set(n,d);
Fraction corrAns=f1-f2;
if(f3==corrAns)
{
correct++;
cout<<"Your answer is correct"<<endl;
}
else
{
wrong++;
cout<<"Your answer is not correct"<<endl;
cout<<"correct answer is: ";
corrAns.reduce();
corrAns.display();
}
}
void divFraction()
{
srand(time(0));
Fraction f1,f2,f3;
f1.set(rand()%15+1,rand()%15+1);
f2.set(rand()%15+1,rand()%15+1);
f1.display();
cout<<" / ";
f2.display();
cout<<" = "<<endl;
int n,d;
cout<<"Enter your answer: ";
cin>>n;
getchar();
cin>>d;
f3.set(n,d);
Fraction corrAns=f1/f2;
if(f3==corrAns)
{
correct++;
cout<<"Your answer is correct"<<endl;
}
else
{
wrong++;
cout<<"Your answer is not correct"<<endl;
cout<<"correct answer is: ";
corrAns.reduce();
corrAns.display();
}
}