I have previously written a program that produces 20 arithmetic problems to solve.
Now I need to change the program so that the problems values (a and b) are written as random integers between 10-99, and the operators are written as +,-,*,/ into an input file... then brought into the console from that input file, then the program runs and the results are displayed in an output file.
I am having trouble getting the values from the input file to the prompt.
In the Values_from_input_file() function, the program does not recognize a,b or op correctly.
For instance when I run the program as is, it displays / = / = / = / = / =.... up to twenty times, which means it is just running the else portion of the while loop over and over.
So my issue is getting a and b to be drawn from the file in the function while recognizing the value of op and displaying the correlating operator for the value of op. e.g. when op == 1, the arithmetic problem should be a + b... when op==2 the problem should read a - b.
The program as it stands, does not recognize a or b correctly from the input file.
Sorry if my code is confusing, I am fairly new to C++.
The objective is to get the function Values_from_input_file() to display the values that are in the input.txt in the correct order and format in the prompt.
The way I thought to do this, was to have it open the file, see the variables a, b, and op, then begin displaying the values and their operator in the prompt one by one for the user to input the answers to.
the values of op are doubles in the file, but they need to act as operators +,-,*, or /.
I know what you need to do, but I am trying to get you to realize your mistake of reading from the file in the if statement rather than cout-ing the values you already read ;)
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
#include <fstream>
usingnamespace std;
void Values_to_input_file();
void Values_from_input_file();
void Find_answer();
int main()
{
srand(time(NULL));
Values_to_input_file();
Values_from_input_file();
return 0;
}
void Values_to_input_file()
{
ofstream v_opfile;
v_opfile.open("input.txt",ios::in);
int count=0;
while (count<20)
{
double a = rand()%(99-10+1) + 10;
double b = rand()%(99-10+1) + 10;
double op = rand()%(4-1+1) + 1;
v_opfile<<a<<" "<<b<<" "<<op<<"\n";
count++;
}
v_opfile<<0<<" "<<0<<" "<<0<<"\n";
v_opfile.close();
}
void Values_from_input_file()
{
double a,b,op,answer;
staticconstchar* answers[] =
{"\nNice Job!\n\n" , "\nCongratulations! Your answer is correct.\n\n", "\nYes! You are right!\n\n"};
ifstream v_opfile;
v_opfile.open("input.txt");
v_opfile>>a;
v_opfile>>b;
v_opfile>>op;
if(op==1)
{
cout<<"\n\n"<<a;
cout<<" + ";
cout<<b;
cout<<" = ";
cin>>answer;
while (answer != (a+b))
{
cout<<"\nSorry, wrong answer. Please try to enter the correct answer again: ";
cin>>answer;
}
if (answer = (a+b))
cout << answers[rand() % 3];
Values_from_input_file();
}
elseif(op==2)
{
cout<<"\n\n"<<a;
cout<<" - ";
cout<<b;
cout<<" = ";
cin>>answer;
while (answer != (a-b))
{
cout<<"\nSorry, wrong answer. Please try to enter the correct answer again: ";
cin>>answer;
}
if (answer = (a-b))
cout << answers[rand() % 3];
Values_from_input_file();
}
elseif(op==3)
{
cout<<"\n\n"<<a;
cout<<" * ";
cout<<b;
cout<<" = ";
cin>>answer;
while (answer != (a*b))
{
cout<<"\nSorry, wrong answer. Please try to enter the correct answer again: ";
cin>>answer;
}
if (answer = (a*b))
cout << answers[rand() % 3];
Values_from_input_file();
}
elseif (op==4)
{
cout<<"\n\n"<<a;
cout<<" / ";
cout<<b;
cout<<" = ";
cin>>answer;
while (answer <= (a/b)-.01 || answer >= (a/b)+.01)
{
cout<<"\nSorry, wrong answer. Please try to enter the correct answer again: ";
cin>>answer;
}
if (answer >= (a/b)-.01 && answer <= (a/b)+.01)
{
cout << answers[rand() % 3];
Values_from_input_file();
}
}
else
cout<<"\n\nthe last problem has been printed.";
}
I am having trouble getting the code to move on to the next a, b, and op values in the function Values_from_input_file()
if the program runs now, it displays a problem such as 14 + 27 and if the user gets the answer correct, the function runs again, but displays the same values for a b and op.
then once a was recognized as 0, the loop stopped and the program continued on.
However i am running into another issue now. I am trying to write the results of the program to an output file. for instance I want to get the a,b,op, problem attempts, whether the input was correct or not, and problem numbers to the output file.
Hi, I did not see the infinite recursion because I was not expecting it.
Your problem is that whenever you open the file for reading it always opens at the start. You have to use the same instance of ifstream or you will lose your progression through the file.
As for outputting to a file, have you check so see if it is_open()?