Generating Math Problems (2)

I made a post about an hour ago with a problem I was having with an assignment, and now I'm having trouble with the second part. Here's what the assignment says:

Write a program that opens the file generate by Program 9a called MathProblems.txt, loops through each problem, solves it and writes the answer to the problem to a file called MathProblemsSolved.txt.

Example: if the input is 1 + 5 , then it would write out 1 + 5 = 6 to the output file.

And here's what I have so far-it's not running and I'm not exactly sure what the problem is. Any help is appreciated!

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
  #include <fstream>  // for ifstream & ofstream
#include <iomanip>  // for setw & setprecision
#include <iostream> // for cout

using namespace std;

int main()
{
    // Data Abstraction
    string mathProblemsFileName = "MathProblems.txt";
    ifstream mathProblemsInput;
    string mathProblemsSolvedFileName = "MathProblemsSolved.txt";
    ifstream mathProblemsSolved;
    int operand1 = 0;               // first operand
    int operand2 = 0;               // second operand

    char theOperator = '+';     // character for operator
    bool filesOpened = 0;    // flag to test if files were opened

    // Attempt to open the newly generate input file with problems
    mathProblemsInput.open("MathProblems.txt");

    if (mathProblemsInput.fail())
    {
        cout << "ERROR:  cannot open ";
             << " for input." << endl << endl;

        bool mathProblemsInput = false;
    }

    if (filesOpened)
    {
        // attempt to open the output file for the solved problems
        mathProblemsSolved.open(mathProblemsSolvedFileName.c_str());

        if (mathProblemsSolved.fail())
        {
            cout << "ERROR:  cannot open " + mathProblemsSolvedFileName << endl;
            bool mathProblemsSolved = false;
        }
    }

    // only continue if the file is opened
    if (filesOpened)
    {
        // loop through the math problems and write the answer
        //   to the final output file
        do
        {
            // read in the math problem
            cin >> operand1 >> theOperator >> operand2;

            // check to see if the EOF was found.
            if ( !mathProblemsInput.eof())
            {
                cout << setw(4) << operand1 << " " << ;
                                   << " " <<  setw(4) << ;
                                   << " = " endl;

                // solve the problem, check for unknown operator
                //      and divide by zero
                mathProblemsSolved << setw(4) << setprecision(8);
                switch ( mathProblemsSolved )
                {
                    case (0) :
                        mathProblemsSolved << operand1 + operand2;
                        break;
                    case (1) :
                        mathProblemsSolved <<  operand1 - operand2;
                        break;
                    case (2) :
                        mathProblemsSolved <<  operand1 * operand2;
                        break;
                    case (3) :
                        if ( operand2 = 0)
                        {
                            cout << "UNDEFINED" << endl;
                        }
                        else
                        {
                            cout << operand1 / operand2;
                        }
                        break;
                    default :
                        // write a message that this is unknown
                        cout << theOperator << "is an unknown operator.";
                        break;
                }
                // add the end of line to the output file
                cout << endl;

            }
        } while( !mathProblemsInput.eof());  // stop when reached EOF
    }

    return 0;
}
There are a few things that are not needed.
1) bool filesOpened = 0; // flag to test if files were opened That is what the if/else are for.

2)bool mathProblemsInput = false; again not needed you have the if/else anyways the two of these variables are local to the if statements so can't be used outside.

You
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    if (mathProblemsInput.fail())
    {
        cout << "ERROR:  cannot open ";
             << " for input." << endl << endl;

        bool mathProblemsInput = false;
    }

    if (filesOpened)
    {
        // attempt to open the output file for the solved problems
        mathProblemsSolved.open(mathProblemsSolvedFileName.c_str());

        if (mathProblemsSolved.fail())
        {
            cout << "ERROR:  cannot open " + mathProblemsSolvedFileName << endl;
            bool mathProblemsSolved = false;
        }
    }
    if (filesOpened)


I would maybe suggest something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    if (!mathProblemsInput.is_open() )
    {
        cout << "ERROR:  cannot open ";
             << " for input." << endl << endl;

    }
    else
    {
        // attempt to open the output file for the solved problems
        mathProblemsSolved.open(mathProblemsSolvedFileName.c_str());

        if (!mathProblemsSolved.is_open() )
        {
            cout << "ERROR:  cannot open " + mathProblemsSolvedFileName << endl;
        }
        else
        {
            //read in data here and output data
        }
    }



You could also do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
//.c_str() if you don't have c++11 enabled
mathProblemsInput( mathProblemsInputFilename ); 
mathProblemsSolved.open( mathProblemSolvedFilename );

if( !mathProblemsInput.is_open() || !mathProblemsSolved.is_open() ) //if either didn't open
{
    cout << "Error: one of the files could not be opened." << endl;
}
else
{
    //read in data
   //output results
}


cin >> operand1 >> theOperator >> operand2; why are you manually inputting the data you are reading it in from the file.

Also to read in all of the data you can simply do:

while( mathProblemsInput >> operand1 >> operator >> operand2 )


then to solve them you are going to have to fix your switch statement:

1
2
3
4
5
6
7
8
9
10
switch( operator )
{
    case '+':
        mathProblemsSolved << operand1 << " + " << operand2 << " = " << operand1 + operand2 << endl;
        break;
    //do same for other operators with necessary fixes
    case '-'; 
    case '*';
    case '/'; //you will have to make sure operand2 is not equal to 0 here
}


Forgot to mention but for operand1 , operand2 , and operator you will want to put the variables that you are using which look to be : operand1 , operand2 , and theOperator
Last edited on
Thanks for your help~
Topic archived. No new replies allowed.