Thread: Simple question

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    Simple question

    Hello all,
    Im a beginner and I need a hint.
    Code:
    // Counter controlled loop
    // Multiplication table
    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    	const int limit = 10;
    	int number;
    	int count = 1;
    	
    	cout << "Multiplication Table"; 
    	cout<< endl << endl;
    
    	cout << "Please enter an integer number: ";
    	cin >> number;
    	
    	while (count <= limit)
    	{
    				cout << count << "times" << number
    				<< "=" << count * number << endl;
    				
    				count++;
    	}
    				
    }
    getch();
    Nice & easy

    I was trying to modify this table by making it print the multiplication table for the numbers 1 to 12.
    I though if I can get the variable number in a nested loop it should do the job.
    After a bit of thinking I come up with this:
    It doesnt give any syntax error but also doest do the job.
    I tried different ways but I guess my lack of knowledge is the problem:
    Code:
    // Counter controlled loop
    // Multiplication table
    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    	const int limit = 10;
    	int number = 1;
    	int count = 1;
    	int counter = 1;
    	const int table = 12;
    	cout << "Multiplication Table"; 
    	cout<< endl << endl;
    	
    	
    	while (number <= table)
    	{
    				
    				
    			
    		while (count <= limit)
    			{
    				cout << count << "times" << number
    				<< "=" << count * number << endl;
    				
    				
    				count++;
    
    			
    			}
    				number++;
    	}
    
    
    		
    	
    }
    getch();
    Any suggestions...

    -Fizz

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You have to remember to set count back to 1 every time you start with a new number.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Of course!! Silly me!
    I cannot belive it.

    Thank a lot for the reply

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which becomes a lot easier if you use a pair of for loops instead of a pair of while loops
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM