Thread: while-loop woes...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    while-loop woes...

    I'm trying to get this while loop to work but I keep getting errors (as usual)
    Here's what I have:

    Code:
    #include <iostream>
    using namespace std;
    
    void instructions ();	
    void calculations ();
    int header;
    int table;
    
    int main ()
    {
    	instructions ();
    	calculations ();
    	header;
    	table;
    	return 0;
    }
    
    void calculations ()
    int header()
    {
    	cout << "TABLE OF NUMBERS AND THEIR SQUARES" << endl;
    	cout << "         Number     Square        " << endl;
    	cout << "         ------     ------        " << endl;
    
    return 0;
    }
    
    int table()
    {
    	int num, square;
    	num = 0;
    
    	while (num <= 5)
    	{
    		square = num * num;
    
    		if (num < 5)
    			cout << "           " << num << "          " << square << endl;
    		else
    			cout << "           " << num << "         " << square << endl;
    
    		num = num + 1;
    	}
    
    	return 0;
    }
    It's supposed to display a table of intergers (1 thru 5) and their squares.
    Thanks for your help.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    if you want the program to display 1-5 why do you initialize num to zero?

    anyhow you are missing an ; after void calculations.
    also, header and table are functions, so why are you declaring them as variables up top?

    replace with

    int header(); and int table();

    Also, you are calling calculations() and instructions from iside main yet they have not been defined anywhere. You do need a function definition even if it is empty. When you call header and table you also forgot the (). DID YOU DO ANY DEBUGGING HERE?!

    what compiler are you using...

    One more thing...if you want a function to display something and not do any calculation make it void....make your calculation functions of the return type.


    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    the brackets.. watch your opening and closing brackets for calculations(). You missed something there dude.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    try this...

    Code:
    #include <iostream>
    using namespace std;
    
    void header();
    void table();
    
    int main ()
    {
      header();
      table();
      return 0;
    }
    
    void header()
    {
      cout << "TABLE OF NUMBERS AND THEIR SQUARES" << endl;
      cout << "         Number     Square        " << endl;
      cout << "         ------     ------        " << endl;
    }
    
    void table()
    {
      int num, square;
      num = 1;
    
      while (num <= 5)
        {
          square = num * num;
    
          cout << "           " << num << "          " << square << endl;
    
          num = num + 1;
        }
    }

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thank you, Pratip

    That's exactly how I needed it to display.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Pratip, could you do my homework? please

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Axon, why not... provided you have to do mine :-) hahaha...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM