CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    61

    minor problem with height

    Can someone take a look at my program for some reason I am unable to get the height to loop and a fresh set of eyes could never hurt:

    This is what the output should be:

    Programmed by <your name>

    Type a negative for the height to exit!
    Enter the height: 7
    Enter the width: 5
    Enter the character to fill the rectangle with: +
    - - - - -
    :+++:
    :+++: (By the way i am trying to make this figure even out in spacing)
    :+++:
    :+++:
    :+++:
    _ _ _ _ _

    Enter the height at the first prompt and the width at the second!
    Type a -1 for the first prompt to exit!
    Enter the height: 1
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: 0
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: 5
    Enter the width: 1
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: -4
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: 2
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: 10
    Enter the character to fill the rectangle with: @

    _ _ _ _ _ _ _ _ _ _
    @@@@@@@:
    @@@@@@@:
    @@@@@@@:
    _ _ _ _ _ _ _ _ _ _

    Enter the height at the first prompt and the width at the second!
    Type a -1 for the first prompt to exit!
    Enter the height: 1
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: -4


    This is what I have:

    Code:
    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number(string prompt);
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom(int width);
    void Draw_Middle(int height, int width, char symbol);
    
    int main()
    {
    	int height, width;
    	char symbol;
    	
    	cout << "Programmed by Jim Johnson";
    	cout << endl << endl;
    	
    	cout << "Type a negative for the height to exit!";
    	cout << endl;
    	
    	do
    	{
    	cout << "Enter the height: ";
    	height = Get_Number("height");
    
    	if (height > 3)
    	{
    		  do
    		  {
    	cout << "Enter the width: ";
    	width = Get_Number("width");
    
    	if ( width <= 3 )
    			 {
                  cout << "Invalid!! Width can not be less than 3." << endl << "Try Again!!!" << endl;
                 } 
              } while (width <= 3 );
           }
             else if ((height < 3) && (height < 3))
              {
                    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again!!!";
              } 
              else
              {
              exit (1);
    		  }
    	         
    	cout << "Enter the character to fill the rectangle with: ";
    	symbol = Get_Character();
    	
    	Draw_Rectangle(height, width, symbol);
    	Draw_Top_Bottom(width);
    	cout << endl;
    	Draw_Middle(height, width, symbol);
    	Draw_Top_Bottom(width);
    
    	cout << endl << "Enter the height at the first prompt and the width at the second!" << endl << "Type a negative for the first prompt to exit!";
    	cout << endl;
    	
    	} while (height >= 3);
    
    	return 0;
    }
    
    int Get_Number(string variable)
    {
    	int x = 0;
    	cin >> x;
    	return x;
    }
    
    char Get_Character()
    {
    	char symbol = 0;
    
    cin >> symbol;
    		
    	return symbol;
    }
    
    void Draw_Rectangle(int width, int height, char symbol)
    {
    
    }
    
    void Draw_Top_Bottom(int width)
    {
    	
    	for (int count = 1; count <= width; count++)
    	cout << '-';
    }
    
    void Draw_Middle(int height, int width, char symbol)
    {
    
    	for(int count = 0; count < height - 2; ++count)
    	
    {
    	cout << '|';
    	for (int count = 1; count < width - 1; ++count)
    	cout << symbol;
    	cout << '|' << endl;
    	}
    }

  2. #2
    Join Date
    Mar 2008
    Posts
    17

    Re: minor problem with height

    Hello,

    Would you mind formatting your code a bit so it is easier to read?

    Especially the indentations...

    Thanks

  3. #3
    Join Date
    Mar 2008
    Posts
    2

    Re: minor problem with height

    Code:
    } while (height >= 3);
    should be:

    Code:
    } while (height >= 0);
    or height!=-1 depending on if you want any negative number or just -1 to be the kill switch




    Code:
    	cout << "Enter the character to fill the rectangle with: ";
    	symbol = Get_Character();
    	
    	Draw_Rectangle(height, width, symbol);
    	Draw_Top_Bottom(width);
    	cout << endl;
    	Draw_Middle(height, width, symbol);
    	Draw_Top_Bottom(width);
    should be moved inside the if (height > 3) statement after the do-while





    Code:
           else if ((height < 3) && (height < 3))
    it doesnt affect anything but this should be changed to:

    Code:
           else if (height < 3)



    Code:
              else
              {
              exit (1);
              }
    can be removed. the do-while will take care of breaking out of the loop




    Code:
    	cout << endl << "Enter the height at the first prompt and the width at the second!" << endl << "Type a negative for the first prompt to exit!";
    	cout << endl;
    can be moved to the first line of the do-while so it doesnt give you the extra message after you enter -1



    i believe that should fix everything. i dont have a compiler so i cant check but if there are any problems with my suggestions it shouldnt be too hard to fix

    edit: added code tags and made it look pretty etc.
    Last edited by Anjar; March 13th, 2008 at 10:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured