Making a math testing program.

Hello, my name is Josh Johnson, and I'm new to this forum. I'm 18 years old, and I took a small class on C++ programming online, so I know the basics of programming. Problem is, it's been a while.

My goal is to make a program where you select your math method (addition, subtraction, or multiplication). After you've selected your method, it goes into a loop where it generates 2 random numbers. And I know it's possible, but I don't know how, but you can set the number of max digits for those random numbers to be. For example, you want to challenge yourself by making it so it will generate 2 digit numbers, so for example 22 + 54 = 76. Or you decide you want even more of a challenge and make it 3 digits, and so on.

Here is where you guys come in. I've tried doing something like this:

Welcome to the math program, select your difficulty:
1. Addition
2. Subtraction
3. Multiplication
Enter 0 to exit.

Mode:

You'd enter the corresponding number, and it would take you to that area of the program. And when I was younger, I did things like this on calculators using labels and goto statements, but upon searching for how to do this, I've learned that this method is frowned upon in the world of programming, so I'm at a loss for how to get you to the area of the program you selected. Once that's done, you get taken into the math part, where you select the difficulty, and you add away. Now I know you guys might be thinking "There is a thing called a search bar, haven't you heard of that?" And yes, I have. I've searched all day for ways to do this, and I just get taken to big confusing sections that have 1 thing I need in the code and I have to pick it out and figure out how it works, and move onto the next one. Well I've found this isn't working at all, and I was hoping that someone could take the time to help me with my problem, but also explain what is going on. Because I'm not someone who just wants someone to do it for me, I want to know how to do it so I can use it in the future.

Thank you for taking the time to read this, and if you will, please help me out. I would appreciate it very much.
Show us what you have so far and we certainly help you with specific problems.

Usually you can replace 'goto' with a loop or 'if'.
I don't have the source code on this computer. So I can't do that. But I can give you code like what I was trying to use. It would have looked something like this:

#include <iostream.h>
#include <cstdlib.h>

int main()
{
int mode, a, b, c, d;
cout << "Math testing program.\n1. Addition\n2. Subtraction\n3. Multiplication\nEnter 0 to quit.\nMode: "
cin >> mode;

if (mode = 1)
goto add;
else if (mode = 2)
goto sub;
else if (mode = 3)
goto mult;
else (mode = 0)
goto quit;

add:
{
(I don't want to type this part out right now since I haven't successfully made it to this point)
}

sub:
{
}

mult:
{
}

end:
{
}

}


It would look something like that. And I recently learned about switches so I could try and replace the if and else part with that. And I've thought about a way to do it now, but I don't know if it's right.

Could I just do something where instead of having labels, I just have a while loop that if mode = 1 it's going to go through the addition loop, and so on? Would something like that work? And then just have it if you enter 0 in the loop, it breaks the loop and then ends the program, or even starts over? But the problem with that, is how would I get it to start over once I've done that?

Last edited on
This isn't what you want, but I coded a small program that should help you somewhat understand how to get this done. This only has one function (MultiplyRandom) that will generate two random numbers with a max of 2 digits (99 max number.) I also have it output the two numbers so you can see that they are random, then return the multiplied result. Goodluck!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <time.h>

int MultiplyRandom()
{
    srand ( time(NULL) );
    int numOne = rand() % 99 + 1; std::cout << numOne << std::endl;
    int numTwo = rand() % 99 + 1; std::cout << numTwo << std::endl;
    return (numOne * numTwo);
}

int main()
{
    int myNum = MultiplyRandom();
    std::cout << "Randomly multiplied number: " << myNum << std::endl;
    return 0;
}

Last edited on
Well I went ahead and coded the entire thing really quick since I'm not doing anything anyway. Hopefully you can understand everything and learn from it. Keep in mind that rand() starts at 0 which is why we use 100 for 2 digits, 1000 for 3 digits, etc.
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <time.h>

void MultiplyFunc(int);
void DivideFunc(int);
void AddFunc(int);
void SubtractFunc(int);

int main()
{
	int myChoice = 1;
	int numDigits = 100;
	std::cout << "-----Menu-----" << std::endl;
	std::cout << "(1): Multiplication" << std::endl;
	std::cout << "(2): Division" << std::endl;
	std::cout << "(3): Addition" << std::endl;
	std::cout << "(4): Subtraction" << std::endl;
	std::cin >> myChoice; 

	std::cout << "Max digits of two random numbers (2-5): ";
	std::cin >> numDigits;

	switch(numDigits)
	{
	case 2:
		numDigits = 100;
		break;
	case 3:
		numDigits = 1000;
		break;
	case 4:
		numDigits = 10000;
		break;
	case 5:
		numDigits = 100000;
		break;
	default:
		numDigits = 100;
		break;
	}

	switch(myChoice)
	{
	case 1:
		MultiplyFunc(numDigits);
		break;
	case 2:
		DivideFunc(numDigits);
		break;
	case 3:
		AddFunc(numDigits);
		break;
	case 4:
		SubtractFunc(numDigits);
		break;
	default:
		std::cout << "ERROR!" << std::endl;
	}
	return 0;
}

void MultiplyFunc(int numDigits)
{
	srand( time(NULL) );
	int numOne = rand() % numDigits;
	int numTwo = rand() % numDigits;

	std::cout << numOne << " " << numTwo << std::endl;
	std::cout << "Your random multiplied number is: " << numOne * numTwo << std::endl;
}

void DivideFunc(int numDigits)
{
	srand( time(NULL) );
	int numOne = rand() % numDigits;
	int numTwo = rand() % numDigits;

	std::cout << numOne << " " << numTwo << std::endl;
	std::cout << "Your random divided number is: " << numOne / numTwo << std::endl;
}

void AddFunc(int numDigits)
{
	srand( time(NULL) );
	int numOne = rand() % numDigits;
	int numTwo = rand() % numDigits;

	std::cout << numOne << " " << numTwo << std::endl;
	std::cout << "Your random added number is: " << numOne + numTwo << std::endl;
}

void SubtractFunc(int numDigits)
{
	srand( time(NULL) );
	int numOne = rand() % numDigits;
	int numTwo = rand() % numDigits;

	std::cout << numOne << " " << numTwo << std::endl;
	std::cout << "Your random subtracted number is: " << numOne - numTwo << std::endl;
}
Last edited on
one thing: if you hit the default in your first switch, your random number will always be either 0 or 1
Updated, thanks.
Last edited on
I understand everything in the int main() part, but as soon as you start dealing with 'void' things, I don't know what's going on. Could you explain how you are using the void parts to make different area's in the program? And also, I'm pretty sure this happens on all computers, but when it runs, after I type the number of digits it uses, it immediately closes. I know that by adding the system("pause") function you can actually see the results, but the "Press any key to continue..." bugs me. What other ways can you keep it from closing before you finish?
Thanks for taking the time to code this, this helps me out a lot, but it would help more if you could briefly explain how exactly the computer determines which area you want to go to. I can see the code that you put to do so, but it doesn't make sense to me.

Thanks for all the help guys, I really appreciate it.
theyre called functions.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

if you want it to stay open you can also just use the getchar function whill will make hte program close when they hit enter and doesnt print an annoying message.

i challenge your method thenoobie. mine supports more possibilities for the number of digits and is shorter :)
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;
using std::endl;
using std::cin;

int main(void)
{
	srand(time(0));
	int choice = 0;
	int digits = 0;
	cout << "Welcome to the math program, would you like to " << endl
		<< "1. Add" << endl
		<< "2. Subtract" << endl
		<< "3. Multiply" << endl;
   cin >> choice;

   cout << "How many digits would you like the numbers to contain?" << endl;
   cin >> digits;

   int num1 = 0;
   int num2 = 0;
   int total = 0;
   int answer = 0;
   for (int i = digits; i > 0; i--)
   {
	   num1 *= 10;
	   num1 += (rand() % 9) + 1;
   }
   for (int i = digits; i > 0; i--)
   {
	   num2 *= 10;
	   num2 += (rand() % 9) + 1;
   }

   cout << "Your numbers are " << endl
	   << num1 << " and " << num2 
	   << endl;

   cout << "What is the total? ";
   cin >> answer;

   switch(choice)
   {
   case 1:
	   total = num1 + num2;
	   break;
   case 2:
	   total = num1 - num2;
	   break;
   case 3:
	   total = num1 * num2;
   }

   if (answer == total)
	   cout << "Thats correct!" << endl;
   else
	   cout << "Sorry!  You're wrong :-(" << endl;

   return 0;
}
Topic archived. No new replies allowed.