File I/O problem

Pages: 12
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.

Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
#include <fstream>

using namespace std;

void Values_to_input_file();
void Values_from_input_file();

int main()
{
    srand(time(NULL));

    Values_to_input_file();

    Values_from_input_file();

    return 0;
}

void Values_to_input_file()
{
    fstream 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()
{
int counter=0;
double a,b,op;

    while (counter<20)
    {
        fstream v_opfile;
        v_opfile.open("input.txt",ios::out);
        v_opfile>>a;
        v_opfile>>b;
        v_opfile>>op;

        if(op==1)
        {
            v_opfile>>a;
            cout<<" + ";
            v_opfile>>b;
            cout<<" = ";
        }

        else if(op==2)
        {
            v_opfile>>a;
            cout<<" - ";
            v_opfile>>b;
            cout<<" = ";
        }

        else if(op==3)
        {
            v_opfile>>a;
            cout<<" * ";
            v_opfile>>b;
            cout<<" = ";
        }


        else
        {
            v_opfile>>a;
            cout<<" / ";
            v_opfile>>b;
            cout<<" = ";
        }

        counter++;

    }
}


I am fairly certain my errors lie within the Values_from_input_file function, but I can't seem to figure it out.

I have op assigned to variables between 1 and 4 in the input file. Is there a way I could display them in the input file as +,-,*,or \?

Any help is greatly appreciated.
random integers between 10-99

Why are you using doubles?
Last edited on
@ L B - Because when the program divides two of the numbers, I want it to keep the decimal.
OK. Next question: what exactly do you think is wrong?
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.
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    while (counter<20)
    {
        fstream v_opfile;
        v_opfile.open("input.txt",ios::out);
        v_opfile>>a; //you read in a...
        v_opfile>>b; //you read in b...
        v_opfile>>op;

        if(op==1)
        {
            v_opfile>>a; //you read in a again?
            cout<<" + ";
            v_opfile>>b; //you read in b again?
            cout<<" = ";
        }
Last edited on
If i remove the top readings, it does not check the value of op before running the if statements, rendering them useless.
Why would you remove the ones before the check?

I must ask...why are you checking the values IN the check??
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 ;)
Okay, thanks for the help L B.

So with your assistance I have made it a bit farther again but am now stuck once more.

Here is the code now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
#include <fstream>

using namespace 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;
    static const char* 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();

        }

        else if(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();

        }

        else if(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();
        }


        else if (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.
I don't see where you're looping anything to make it do more than one problem per program.
when Values_from_input_file(); calls itself in the 4 places where op==1,2,3,4 respectively.

lines 76, 96,116,137.

The function calls the same function again, but the issue is that it reads the same a b and op values that it did on the first run through.
Last edited on
Ok, I figured out a way to get the function to read the next set of values from the input file.\

Here is a section of the code i used to do so...

1
2
3
4
5
6
7
8
9
    ifstream v_opfile;
    v_opfile.open("input.txt");

    while(a != 0)
    {

        v_opfile>>a;
        v_opfile>>b;
        v_opfile>>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.

I tried using:

1
2
    ofstream output_file;
    output_file.open("output.txt");



followed by this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  while(a != 0)
    {

        v_opfile>>a;
        v_opfile>>b;
        v_opfile>>op;
        problem_number++;
        output_file<<problem_number<<"  ";

        if(op==1)
        {
            cout<<"\n\n"<<a;
            cout<<" + ";
            cout<<b;
            cout<<" = ";
            cin>>answer;
            output_file<<a<<"  "<<op<<"  "<<b;

            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];

        }

        else if(op==2)
        {
            cout<<"\n\n"<<a;
            cout<<" - ";
            cout<<b;
            cout<<" = ";
            cin>>answer;
            output_file<<a<<"  "<<op<<"  "<<b;

            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];

        }

        else if(op==3)
        {
            cout<<"\n\n"<<a;
            cout<<" * ";
            cout<<b;
            cout<<" = ";
            cin>>answer;
            output_file<<a<<"  "<<op<<"  "<<b;

            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];

        }

        else if (op==4)
        {
            cout<<"\n\n"<<a;
            cout<<" / ";
            cout<<b;
            cout<<" = ";
            cin>>answer;
            output_file<<a<<"  "<<op<<"  "<<b;

            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];
            }
        }

    }
        cout<<"\n\nthe last problem has been printed.\n\n";
}


but when I open up the output file, it is empty, and while looking through examples and such online, I cannot seem to find where my issue lies.

Any help regarding this issue is greatly appreciated.

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()?
Last edited on
Yes I did check, here is the code portion that opens it.

1
2
    ofstream output_file;
    output_file.open("output.txt");


to check it i just used

1
2
3
4
    if (output_file.is_open())
    {
        cout<<"its open!";
    }


and the program did display that it was open.
If you delete the output file does it make a new one? If it doesn't it is probably making it somewhere else.
yes it does.

do you think I have to run the entire program and do all twenty problems for the output to show up?
Maybe, I don't think ofstream is required to write to the file instantly, it may only do it upon flush or close.
I ran the whole program through and was able to see values in the output file...

but only 4 values were displayed.

They were something like " 1 32 88 10" (i don't remember exactly and have ran parts the program since)

any ideas as to why only 4 values were displayed?

was the output file just writing data over itself? For instance, was it just writing the last values it saw to a, b and op respectively?
Pages: 12