Generating Math Problems

Hello!

I'm having a little bit of trouble getting this program to run and show the correct output. Here is what I was assigned to do:

Write a C++ program that generates 10 multiplication, subtraction, division, addition problems in algebraic notation, writing the problems to an output file called MathProblems.txt in algebraic notation which is the "normal" format

operand1 operator operand2

Examples :

5 + 1 is 5 + 1 = 6

10 / 2 is 10 / 2 = 5


Basically the program iscreating a file that will be opened by another code and each problem will be solved. Here is what I have 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
  #include <cstdlib>  // for rand and srand
#include <fstream>  // for istream and ofstream
#include <iostream> // for cout

using namespace std;

const int NUMBER_OF_PROBLEMS = 10;  // the number of problems to generate
const int MAX_OPERAND = 20;          // the max operand
const int MAX_OPERATORS = 4;         // the number of operators

int main()
{
    // Data Abstraction
    string mathProblemsFileName = "MathProblems.txt";
    ofstream mathProblemsOutput;
    int problemCounter = 0;  // accumulator to count the number of lines
                             // generated

    char theOperator = '+';     // character for operator

    // open file for output
    mathProblemsOutput.open(mathProblemsFileName.c_str());

    // check if the file opened
    if ( mathProblemsOutput.fail())
    {
        cout << "ERROR:  cannot open " << mathProblemsFileName
             << " for output." << endl;
    }
    else
    {
        // generate the math problems and write to a text file
        srand(0);

        for (problemCounter; problemCounter < 10; problemCounter++)
        {
            switch ( rand() % 99)
            {
                case(0) :  // set the operator to '+'
                    theOperator;
                    break;
                case(1) :  // set the operator to '-'
                    theOperator;
                    break;
                case(2) :  // set the operator to '*'
                    theOperator;
                    break;
                case(3) : // set the operator to '/'
                    theOperator;
                    break;
                default :
                    break;
            }

            theOperator << rand() % (10);
                    << " "
                    << theOperator
                    << " "
                    << 0 % (10) << endl;
        }

        mathProblemsOutput.close();
    }

    return 0;
}


Any help is appreciated!
Last edited on
You are going to generate the same problems over and over again each time you run the program.

1
2
        // generate the math problems and write to a text file
        srand(0);



This is wrong in a few ways:
1) you have choices from 0-3 but you are using mod 99 instead of mod 4.
2) you aren't actually setting the operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
            switch ( rand() % 99)
            {
                case(0) :  // set the operator to '+'
                    theOperator;
                    break;
                case(1) :  // set the operator to '-'
                    theOperator;
                    break;
                case(2) :  // set the operator to '*'
                    theOperator;
                    break;
                case(3) : // set the operator to '/'
                    theOperator;
                    break;
                default :
                    break;
            }


maybe you want something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
            switch ( rand() %4 )
            {
                case(0) :  // set the operator to '+'
                    theOperator = '+';
                    break;
                case(1) :  // set the operator to '-'
                    theOperator = '-';
                    break;
                case(2) :  // set the operator to '*'
                    theOperator = '*';
                    break;
                case(3) : // set the operator to '/'
                    theOperator = '/';
                    break;
                default :
                    break;
            }


This part you probably want to output to the file and not shift the bits (if it even compiles). Also the second operand is always going to be zero.

1
2
3
4
5
            theOperator << rand() % (10);
                    << " "
                    << theOperator
                    << " "
                    << 0 % (10) << endl;


something like this maybe:

1
2
3
4
5
             mathProblemsOutput << theOperator << rand() % (10);
                    << " "
                    << theOperator
                    << " "
                    << rand() % (10) << endl;
Thanks for the help, got it to run well! I just posted another question about part 2 of this problem, if you wouldn't mind taking a look and helping me with that too? Thanks again!
Topic archived. No new replies allowed.