Thread: Keen on Math

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Question 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. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    break only exits one loop, not two.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Hi,
    Instead of using
    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.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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. #5
    Or, use break [to where];
    Code:
    out:
    for(;;){
      for(;;){
        break out;
      }
    }
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Inquirer
    Or, use break [to where];
    Code:
    out:
    for(;;){
      for(;;){
        break out;
      }
    }
    ~Inquirer
    goto, you mean.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Don't think so, in my C++ book (i forget the name, but it is by herbert schildt) it shows almost this example.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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"
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Smile

    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. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM