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:
#include <cstdlib> // for rand and srand
#include <fstream> // for istream and ofstream
#include <iostream> // for cout
usingnamespace std;
constint NUMBER_OF_PROBLEMS = 10; // the number of problems to generate
constint MAX_OPERAND = 20; // the max operand
constint 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;
}
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.
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!