Thread: help modifying a program...

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

    help modifying a program...

    I have this program that works by asking the user to determine what size box they would like displayed. It asks 7 times.
    What I need to do is modify it so it just prints the output without asking the user. (that's what happens when you don't read the assignment thoroughly!)
    The output should say:
    A box of 3 is shown below:
    * * *
    * * * (without the * in the middle)
    * * *

    It;s supposed to display boxes sized 5, 4, 3, 2, 1, then error messages for 0 and -1.

    I got the code working but now I don't know how to change it to just display everything without asking the user to specify the size.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int centspace();
    int counter();
    
    //Functions used ...
    void instructions ();	//User instructions
    int box ();
    
    //-------------------------------------------------------
    
    int main ()
    {
                    instructions ();
    	box ();
    	box ();
    	box ();
    	box ();
    	box ();
    	box ();
    	box ();
    
    	cout << endl;
    
    	return 0;
    }
    
    //--------------------------------------------------
    
    int box ()
    {
    	int size;
    	cout << "enter a number: ";
    	cin >> size;
    	if ((size <= 0) || (size >= 40))
    	{	
    	cout << "It is not possible to print a box of size "
                           << size << endl;
    	}
    	else
    	{
    	for (int counter = 1; counter <= size; counter++)
    	     cout << "* ";
    	cout << endl;
    	if (size > 2)
    	{
    	     for (int counter = 1; counter <= (size-2); counter++)
    	{
    	    cout << "*";
    	for (int space = 1; space <= (size-2); space++)
    	    cout << "  ";
    	cout << " *" << endl;
    	}
    	}
    	if (size > 1)
    	{
    	for (int counter = 1; counter <= size; counter++)
    	{
                        cout << "* ";
    	}
    	cout << endl;
    	}
    	}
    	return 0;
    }
    sorry if this is hard to read...everything moved when I pasted it.

    Any point in the right direction would help me tremendously. Thanks!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Ahh, looks like the perfect time for a function parameter. For example:
    Code:
    #include <iostream>
    
    void Display(int);
    
    int main()
    {
        int myNum = 0;
        Display(myNum);
        Display(myNum+1);
        Display(2);
        Display(-1);
        myNum = 3;
        Display(myNum);
        return 0;
    }
    
    void Display(int num)
    {
        if (num <= 0 || num >= 6)
        {
            std::cout << "Error, " << num << " is invalid input." << std::endl;
        }
        else
        {
            std::cout << "Number is " << num << std::endl;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM