I'm up to finish an assignment for my C++, but there must be some thing wrong with the codes....
hope someone will able to give me a hand~

so...here's the title assignment:
Program generates a random math problem, dealing with the : addition, subtraction, multiplication, or division of integer values.
The value ranges for the operand are:

operand 1    operand 2
addition         101-200      101-200
subtraction      101-200      101-200
multiplication    11-20        11-20
division         101-200       11-20

The program randomly picks the operator type and operand values,displays the question, waits for the user to input an answer, then then either tells the user they got the answer correct (if they did) or asks them if they wish to see the correct answer (to which they can respond Y or N for yes or no) if they do wish to see the correct answer the program should display both the original problem and the answer the program should then exit!

And here are my codes: (although its just half done....)

include<iostream>
include<cstdlib>
include<ctime>
using namespace std;



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

const int PLUS=0; //const PLUS as 0
const int MINUS=1; //const MINUS as 1
const int DIVIDE=2; //const DIVIDE as 2
const int MULTIPLY=3; //const MULTIPLY as 3

int a, b, op;

a = rand() % 200 + 101; //range 101-200
b = (rand() % 200) + 101; //range 101-200
op = rand() % 4; //the 0-3 to match an operation

int answer, userGuess; //right answer

switch(op)
{
case PLUS:
answer = a + b;
cout << "What is " << a << " + " << b << "? ";
cin >> userGuess;
break;

case MINUS:
answer = a - b;
cout << "What is " << a << " - " << b << "? ";
cin >> userGuess;
break;

case DIVIDE:
answer = a / b;
cout << "what is " << a << " / " << b << "? ";
cin >> userGuess;
break;

case MULTIPLY:
answer = a * b;
cout << "what is " << a << " * " << b << "? ";
cin >> userGuess;
}

if(answer == userGuess)
cout << "you are correct." << endl;

return;
}

sorry I'm a noob...but :'(please help me out if you are there. THX

Edited 5 Years Ago by davidhk35: n/a

The function main() expects a return value. Other than that your code works fine....

return 0;

Edited 5 Years Ago by vidit_X: n/a

so far, it works great on the addition and substations. but Im totally stuck on the division and multiplication...how would it works with the different operators and operands values in this situation? thx

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main ()
{
srand(time (0));

const int PLUS=0; //const PLUS as 0
const int MINUS=1; //const MINUS as 1
const int DIVIDE=2; //const DIVIDE as 2
const int MULTIPLY=3; //const MULTIPLY as 3

int a, b, op;

a = rand() % 200 + 101; //range 101-200
b = (rand() % 200) + 101; //range 101-200
op = rand() % 4; //the 0-3 to match an operation

int answer, userGuess; //right answer

switch(op)
{
case PLUS:
answer = a + b;
cout << "What is " << a << " + " << b << "? ";
cin >> userGuess;
break;

case MINUS:
answer = a - b;
cout << "What is " << a << " - " << b << "? ";
cin >> userGuess;
break;

case DIVIDE:
answer = a / b;
cout << "what is " << a << " / " << b << "? ";
cin >> userGuess;
break;

case MULTIPLY:
answer = a * b;
cout << "what is " << a << " * " << b << "? ";
cin >> userGuess;
}

if(answer == userGuess)
cout << "you are correct." << endl;
else
cout << "You are incorrect" << endl;


getch();
}

The range of multiplication and division operands are different than the addition and subtraction operands.

So you need to generate separate operands for them.

so....does that mean I need to generate another c and d instead of a and b?
a and b for + and -
c and d for * and / ?
is that right?

also....one more thing wanna ask... how will it finish up with showing the functions and answers for the user if he got the wrong answer?
is it gonna be like this?

int yes, Y; //user wanna check the problem and answer
int no, N; //user dones't need the answer

if(answer == userGuess)
cout << "Congratulations!! You are correct." << endl << "GoodBye!" << endl;
else
cout << "Too bad. You are incorrect!!" << endl;
cout << "Would you like to see the correct answer (Y or N)?" <<endl;
cin >> Y;
if(yes == Y)
cout << " = answer"

Edited 5 Years Ago by davidhk35: n/a

Yes, that is right. You need to generate another c and d.

Since you need to display the question too if user is wrong. You need to store the operation selected during previous switch. And a little correction in your code..

else
{ 
   cout << "Too bad. You are incorrect!!" << endl;
   cout << "Would you like to see the correct answer (Y or N)?" <<endl;
   cin >> Y;
   if(yes == Y)
     cout << "Question"<< ... <<"\nAnswer "<<answer;
   ...
} //You forgot the blockquotes

Edited 5 Years Ago by vidit_X: n/a

but how will I able to find out the question which is generated randomly by the computer?

I need to int the question, XXX <---- If you don't mind, would you teach me please? thx

You need not store the question, its already in variables. Just use..

if(op == PLUS)
    cout<<"Question "<<a<<" + "<<b<<"\nAnswer "<<answer;

Edited 5 Years Ago by vidit_X: n/a

which means I have to type if(op -- PLUS) for the case 1 only....do I have to type in for the 3 others as well? like the minus, multiply and division?

Yes, this is what I can think. There might be other ways. Post your code after adding the above..

Edited 5 Years Ago by vidit_X: n/a

okay, I will post it up again when its done.. but it might take a long time for me <----super rookie
thankyou so much!

YES!! FInally done with my codes.....have a peek ^^ THX for everyones help!

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;



int main ()
{
srand(time (0)); // Initialize random number generator

const int addition=0; //const PLUS as 0
const int subtraction=1; //const MINUS as 1
const int division=2; //const DIVIDE as 2
const int multiplication=3; //const MULTIPLY as 3
char response; //response stand for Y/N

int a, b, c, op;
int answer, userGuess; //user type in or input, cin
a = (rand() % 200-101) + 101; //range 101-200
b = (rand() % 200-101) + 101; //range 101-200
c = (rand() % 20-11) + 11; //range 11-20
op = rand() % 4; //the 0-3 to match an operation



cout << "Please enter your answer for the question" << endl; //Instrution

switch(op) //switching system fot the random picking operator
{
case addition: //1st case for adding
answer = a + b; //answer will be a+b
cout << "    What is " << a << " + " << b << "? " << endl; //Question's given
cout << "Answer:";
cin >> userGuess; //user type in
break;

case subtraction: //2nd case for minus
answer = a - b; //answer will be a-b
cout << "    What is " << a << " - " << b << "? " << endl; //Question's given
cout << "Answer:";
cin >> userGuess; //user type in
break;

case division: //3rd case for dividing
answer = b / c; //answer will be b/c
cout << "    what is " << b << " / " << c << "? " << endl; //Question's given
cout << "Answer:";
cin >> userGuess; //user type in
break;

case multiplication: //4th case for times
answer = c * c; //answer will be c*c
cout << "    what is " << c << " * " << c << "? " << endl; //Question's given
cout << "Answer:";
cin >> userGuess; //user type in
}



if(answer == userGuess){
cout << "Congratulations!! You are correct." << endl << "GoodBye!" << endl;
} else{
cout << "Too bad. You are incorrect!!" << endl;
cout << "Would you like to see the correct answer (Y or N)?" << endl;
cin >> response;


}if(response == 'Y'){
if(op == addition) //op case1
cout <<"Question "<<a<<" + "<<b<<"\nAnswer "<< answer <<endl;
if(op == subtraction) //op case2
cout <<"Question "<<a<<" - "<<b<<"\nAnswer "<< answer <<endl;
if(op == division) //op case3
cout <<"Question "<<b<<" / "<<c<<"\nAnswer "<< answer <<endl;
if(op == multiplication) //op case4
cout <<"Question "<<c<<" * "<<c<<"\nAnswer "<< answer <<endl;
} else if(response == 'N'){
cout << "GoodBye!" << endl;
}


return 0;
}

Edited 5 Years Ago by davidhk35: n/a

This article has been dead for over six months. Start a new discussion instead.