I\'m having troubles running this program, why can\'t I get this c++ code to run
ID: 3800716 • Letter: I
Question
I'm having troubles running this program, why can't I get this c++ code to run? I'm getting 32 error messages.
#include
#include
#include
using namespace std;
int main()
{
//declare variables
const int MAX_RANGE = 100;
int randomNumber1, randomNumer2
double result;
int choice;
int answer;
bool selection = true
//continue loop till user quits
do
{
unsigned seed = time(0);
srand(seed);
//generate random numbers
randomNumber1 = 1 + rand() % MAX_RANGE;
randomNumber2 = 1 + rand() % MAX_RANGE;
//menu
cout << "----MENU----"< cout << "1. Addition"< cout << "2. Subtraction"< cout << "3. Multiplication"< cout << "4. Division"< cout << "5. QUIT"<
cout << "Enter your choice (1/2/3/4/5)";
cin >> choice;
switch(choice)
{
case 1:
cout << "Addition"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "+" << setw(3) << randomNumber2
<< endl;
cout << "----" << endl;
result = randomNumber1 + randomNumber2;
break;
case 2:
cout << "Subtraction"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "-" << setw(3) << randomNumber2
< cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
case 3:
cout << "Multiplication"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "*" << setw(3) << randomNumber2
< cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
case 4:
cout << "Division"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "/" << setw(3) << randomNumber2
< cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
//Quit
case 5:
system("pause");
exit(0);
default:
cout << "Enter a choice from the menu (1/2/3/4/5)"
<< endl;
selection = false
}
if(selection)
{
cout << "Enter your answer.";
cin >> answer;
if(answer == result)
cout << "Good Job! Your answer is correct!" << endl;
else
cout << "Sorry, your answer is wrong.
Thecorrect answer is: " << setw(3)
<< result << endl;
}
selection = true;
cout << endl;
}while(selection);
system("pause");
return 0;
}
Explanation / Answer
There were multiple errors in code:
1. Header files were missing
2. Semi colons were missing
3. cout command was syntactically incorrect
4. Variable names were missing
5. Paranthesis were missing
Working code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
//declare variables
const int MAX_RANGE = 100;
int randomNumber1, randomNumber2;
double result;
int choice;
int answer;
bool selection = true;
//continue loop till user quits
do
{
unsigned seed = time(0);
srand(seed);
//generate random numbers
randomNumber1 = 1 + rand() % MAX_RANGE;
randomNumber2 = 1 + rand() % MAX_RANGE;
//menu
cout << "----MENU---- " << "1. Addition " << "2. Subtraction "<< "3. Multiplication " << "4. Division " << "5. QUIT ";
cout << "Enter your choice (1/2/3/4/5)";
cin >> choice;
switch(choice)
{
case 1:
cout << "Addition"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "+" << setw(3) << randomNumber2 << endl;
cout << "----" << endl;
result = randomNumber1 + randomNumber2;
break;
case 2:
cout << "Subtraction"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "-" << setw(3) << randomNumber2;
cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
case 3:
cout << "Multiplication"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "*" << setw(3) << randomNumber2;
cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
case 4:
cout << "Division"<< endl;
cout << setw(5) << randomNumber1 << endl;
cout << "/" << setw(3) << randomNumber2;
cout << "----" << endl;
result = randomNumber1 - randomNumber2;
break;
//Quit
case 5:
system("pause");
exit(0);
default:
cout << "Enter a choice from the menu (1/2/3/4/5)" << endl;
selection = false;
}
if(selection)
{
cout << "Enter your answer.";
cin >> answer;
if(answer == result)
cout << "Good Job! Your answer is correct!" << endl;
}
else{
cout << "Sorry, your answer is wrong. Thecorrect answer is: " << setw(3)<< result << endl;
}
selection = true;
cout << endl;
}while(selection);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.