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.
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?
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!
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.
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.
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 :)