# Thread: Keen on Math

1. ## Keen on Math

Hi please help my program listing 2 digits multiplication 10 lines on screen, pauses and ask if you want to continue or quit by pressing 'Q' it should quit but it doesn't. It could be a very simple problem, but I just cannot figure it out. Please help if you can, thanks.

Code:
```int main()
{
clrscr();
cout << "\t\t\tTWO DIGITS MULTIPLICATION" << endl;
cout << "\t\t\t=========================" << endl;

for (int num1 = 11; num1 <= 99; num1++)
{
for (int num2 = 11; num2 <= 99; num2++)
{
cout << "\t\t\t" << num1 << " * " << num2 << " = " << num1  * num2 <<endl;
if (num2 % 10 == 0)
{
cout << "\n\t\t\tPress enter to continue, Q to quit.";
char getkey=toupper(getch());
if (getkey == 'Q') break;
cout << "\n\n";
}
}
cout << endl;
}
return(0);
}```

2. break only exits one loop, not two.

3. Hi,
Code:
`if (getkey == 'Q') break;`
you should use
Code:
`if(getkey == 'Q') exit(0);`
I'm not sure in which library it is. I'm using it in my program and doesn't give me any problem.

4. stdlib.h or cstdlib contains exit(); You could also use return 0;
If you just want to break out of the loops, a goto is permissable in this situation.

5. Or, use break [to where];
Code:
```out:
for(;;){
for(;;){
break out;
}
}```
~Inquirer

6. Originally posted by Inquirer
Or, use break [to where];
Code:
```out:
for(;;){
for(;;){
break out;
}
}```
~Inquirer
goto, you mean.

7. Don't think so, in my C++ book (i forget the name, but it is by herbert schildt) it shows almost this example.

8. Originally posted by Inquirer
Don't think so, in my C++ book (i forget the name, but it is by herbert schildt) it shows almost this example.
Maybe you should look for your book review on here and see if it is "recommended" or not. HS has a lot of books that are "Not Recommended"

9. Thank you guys for your comments. I've got it sorted after reading Magos' corrrection that a break, breaks out only of one loop at a time, so I only needed to add a flag in the inner loop to indicate that a 'q' has been pressed and a desire to break out the outter loop to be effected.

SoKrA's suggestion would hault the program completely, which I don't want the program to do.

eibro's suggestion to just use return only is interesting, which I will try, not sure if that will work.

Anyway, thank you all tha same.
=============================================
Be nice, be kind, be cheerful, life's too short to be miserable!

10. return 0; directly in main will function pretty much like exit(0), and exit the whole program.
You could place your code in a sub function and then use return to exit that function, but not main (thus allow you to do other stuff after the loops).

Popular pages Recent additions