Thread: Multiplication Table Error

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Exclamation Multiplication Table Error

    Hello,

    I am trying to make a multiplication table that goes up to 10 X 10 = 100. I am a newbie in C++ and this is probably a really stupid error.

    Code:
      #include <iostream.h>
    
      int main()
      {
    	  int num = 1;
    	  int num1 = 1;
    	  int num2 = 2;
    	  int num3 = 3;
    	  int num4 = 4;
    	  int num5 = 5;
    	  int num6 = 6;
    	  int num7 = 7;
    	  int num8 = 8;
    	  int num9 = 9;
    	  int num10 = 10;
    
    	  do
    	  {
    		  num1 = num++;
    
    		  if ( num1 < 10)
    			  cout << "00" << num1 << " ";
    		  if ( ( num1 >= 10 ) & ( num1 < 100 ) )
    			  cout << "0" << num1 << " ";
    	  }
    	  while ( num1 != 10 );
    
    	  cout << endl;
    
    
    	  num = 1;
    
    	  do
    	  {
    		  num2 = num2 * num++;
    
    		  if ( num2 < 10)
    			  cout << "00" << num2 << " ";
    		  if ( ( num2 >= 10 ) && ( num2 < 100 ) )
    			  cout << "0" << num2 << " ";
    	  }
    	  while ( num2 != 20 ); 
    
    
    
    	  return 0;
      }
    This displays alot of 000's.
    Can you please explain to me what i did wrong?
    Thanks in advance to all those who help me.

  2. #2
    Unregistered
    Guest
    Are you trying to make a mult. table like 10 * 1, 10 * 2, etc. to 10 *10? If you are think about which number is changing or increasing and how much is it increasing. Then use a for statement.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Code:
    num = 1;
    
    do
    {
        num2 = num2 * num++;
    	
        if ( num2 < 10)
            cout << "00" << num2 << " ";
        if ( ( num2 >= 10 ) && ( num2 < 100 ) )
            cout << "0" << num2 << " ";
    }  while ( num2 != 20 );
    Here things go wrong.

    Code:
    num    num2    (num2 = num2 * num++)
    1         2           2
    2         2           4
    3         4           12
    4         12         48
    So num2 will never become 20.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Talking

    Hmm, you actually just need two do//while statements and two counters to solve this problem.

    ....
    ....
    int num = 0;
    int num1 = 0;
    do
    {
    //Increase "left" number by one
    num++;
    do
    {
    //Increase "right" number by one
    num1++;
    cout << num << " * " << num1 << " = " << (num*num1) << endl;
    }while ( num1 != 10 );
    //When num1 has reached 10 then set it to 0 again
    num1 = 0;
    cout << endl;
    }while ( num != 10 );
    ....


    You can even use for-loops to achive the same result, its even better(shorter).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM