Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write your program to test arithmetic skills of children so that it uses file in

ID: 3637963 • Letter: W

Question

write your program to test arithmetic skills of children so that it uses file input and functions. The
program will ask the student to solve a set of arithmetic problems. The problems will be contained in a
file. The last line of the file will be
0 0 0
and will signal that there are no more problems to be worked.
For each problem, the program will read the question from the file, display it on the terminal, and expect
the student to enter the answer. If the answer is correct, it will randomly display (on the terminal) one of
the following messages: “Nice Job!”, “Congratulations! Your answer is correct.” and “Yes! You are
right!”. It will then write to an output file the problem number (1,2,3,…), the problem, whether or not the
student got the correct answer, and the number of attempts the student made. Then it will get the next
problem from the file. However, if the answer is not correct, the program will display “Sorry, wrong
answer. Please try to enter the correct answer again:”, and read the answer and check again, until the
correct answer is entered. (You can limit the number of attempts to 5 if you choose).
When all problems have been entered, the program should write to the file the number of problems, the
number the student eventually got correct, and the average number of tries.
The arithmetic problems will look like the following:
99 + 55 = ?
33 - 11 = ?
43 * 23 = ?
71 / 12 = ?
Basically, each problem consists of two integer operands and one operator, all of the problems will be in
the input file. The operands should be between 10-99. The student is expected to perform a floating
point calculation, so you will need to do some type conversion. As long as the difference between the
answer and the key is less than 0.01, it is counted as correct.
Your program should use the following functions:
findanswer(number1, number2, operator): calculates the correct answer to a given problem
check (answer1, answer2): decides if the student entered the correct answer
printmessage( goodanswer): if the answer is correct, randomly choose one of the positive messages; if
the answer is incorrect, print the appropriate message
example input file:
99 55 +
33 11 -
43 23 *
71 12 /
0 0 0
Example output file:
arithmetic practice
problem # op1 operator op2 correct? # attempts
1 99 + 55 yes 1
2 33 - 11 yes 1
3 43 * 23 yes 2
4 71 / 12 no 5
#attempted #correct average # tries
4 3 2.25

Explanation / Answer

please rate - thanks

message me if any problems.

it wasn't clear what to do when doesn't get it right after 5 tries, since the wrong message always says try again

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
double findanswer(int,int,char);
bool check(double,double);
void printmessage(bool);
int main()
{
ifstream in;
ofstream out;
char filename[80];
int num1,num2,tries,problems=0,totaltries=0,gotright=0;
bool good;
char op;
double correct,user;
srand(time(0));
cout<<"Enter name of input file: ";
cin>>filename;
in.open(filename);       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   system("exit");
   }
cout<<"Enter name of output file: ";
cin>>filename;
out.open(filename);
out<<"problem # op1 operator op2 correct? #attempts ";
in>>num1;
while(num1!=0)
   {problems++;
    in>>num2>>op;
    correct=findanswer(num1,num2,op);
    tries=0;
    cout<<"new problem ";
    do
      {cout<<num1<<" "<<op<<" "<<num2<<" = ? ";
       cin>>user;
       tries++;
       good=check(correct,user);      
       }while(!good&&tries<5);
       if(!good)
          cout<<"Correct answer is: "<<setprecision(2)<<fixed<<correct<<endl;
       totaltries+=tries;
       out<<problems<<" "<<num1<<" "<<op<<" "<<num2<<" ";
       if(good)
           {gotright++;
           out<<"yes";
           }
       else
           out<<"no";
       out<<" "<<tries<<endl;
           
    in>>num1;
   }
out<<"#attempted #correct average # tries ";
out<<" "<<problems<<"    "<<gotright<<"    "<<setprecision(2)<<fixed<<totaltries/(double)problems<<endl;
out.close();
in.close();
system("pause");
return 0;
}
bool check(double answer1,double answer2)
{int a1,a2;
bool good;
a1=(int)(answer1*100);
a2=(int)(answer2*100);
if(a1!=a2)
     good=false;
else
     good=true;
printmessage(good);
return good;
}
void printmessage(bool good)
{if(!good)
     cout<<"Sorry, wrong answer. Please try to enter the correct answer again: ";
else
    {switch(rand()%2)
      {case 0: cout<<"Nice Job! "; break;
       case 1: cout<<"Congratulations! Your answer is correct. "; break;
       case 2: cout<<"Yes! You are right! "; break;
       }
    }
}
double findanswer(int number1, int number2, char oper)
{switch (oper)
   {case '+': return (double)(number1+number2);
    case '-': return (double)(number1-number2);
    case '*': return (double)(number1*number2);
    case '/': return (double)number1/number2;
   }
}