>>I tried writing some code for this, but am having way to many issues
please post code, and use code tags as described in the links in my signature below.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Here;s something you can work on. It may not be exactly what you need but its a basis at least. Also, why do you need to check if the second number of the division is zero, from the second you say it will accept values from 1-20?? Anyway, I put the check in the program as well if u need it for later on.. Hope it helps, byee
#include <iostream>
#include <ctime>
using namespace std;
int DoMenu();
void addition(int random1, int random2);
void subtraction(int random1, int random2);
void multiplication(int random1, int random2);
void division(int random1, int random2);
int main()
{
int random1, random2;
srand(time(0));
int choice;
do
{
random1 = 1+(rand()%20); // from 1-20
random2 = 1+(rand()%20); // from 1-20
choice = DoMenu();
switch (choice)
{
case 0: cout << endl; break;
case 1: addition(random1,random2); break;
case 2: subtraction(random1,random2); break;
case 3: multiplication(random1,random2); break;
case 4:
{
if (random2 == 0)
cout << "Error: Division by zero, cannot proceed\n";
else
division(random1,random2);
} break;
default : cout << "Error in input\n\n"; break;
}
} while (choice != 0);
return 0;
}
int DoMenu()
{
int MenuChoice;
cout << "(1)Addition\n";
cout << "(2)Subtraction\n";
cout << "(3)Multiplication\n";
cout << "(4)Division\n";
cout << "(0)Quit\n";
cout << "-----------------\n";
cout << "Choice: ";
cin >> MenuChoice;
return MenuChoice;
}
void addition(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " + " << random2 << " = ";
cin >> answer;
if (answer == (random1+random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void subtraction(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " - " << random2 << " = ";
cin >> answer;
if (answer == (random1-random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void multiplication(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " * " << random2 << " = ";
cin >> answer;
if (answer == (random1*random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void division(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " / " << random2 << " = ";
cin >> answer;
if (answer == (random1/random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
I see in your code that you are not using srand( ) to seed or to initialize your random function and hence you are getting repetitive values.
Try putting srand( time(NULL) ) at the start of your code and then see. For more description see here.
HOpe it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Okay the thing is that while generating random numbers care has to be taken that the numbers so generated dont repeat in a pattern or frequently (though this would happen if the range is small).
To safeguard from this, normally all random generators make use of a base values (normally a very large number) which serves as a base or as a reference so that the numbers so generated using them dont repeat.
This said, care should be taken that the reference number should be as unique as possible and should not be ideally repeated.
The time( ) function returns the number of seconds that have passed since 00:00:00 GMT January 1, 1970. So when this value is passsed to the time function, it serves as a satisfactory reandom number generator reference or seed value as they call it.
More information here.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
hey, so i also had a question about this as well but it's an extension to his basic math tutor problem. i used the code layout that may4life gave out which was fine and dandy but how do i get the program to where the user can practice math problems continuously until he/she decides to quit the program? i am teaching myself how to code but it's a slow process and the book and instructions aren't very clear to me and it's asking for this and have it in a do-while loop.it says the program should output the number of problems attempted, number of correct answers, and the score as a percentage as well. can anyone help me out? it's an online course as well so i'm sortof stuck on time a bit so any help here by sometime tonight would be very much appreciated. do i just make the do-while loop after typing in the if/else statements or after each if/else statement? sorry if this is the wrong place to be asking this and if it is then please let me know and let me know how to take care of my question so i can either move it or whatever i need to do. i'm just tryin to understand all this but i'm so much of a code noob that it's hard for me i guess. idk =/
Add extra while loop , ask user whether he wants to quit then break from while loop.
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128