Outputting a shape using C++

Discussion in 'Mac Programming' started by USCtrojan08, Sep 3, 2009.

  1. USCtrojan08 macrumors newbie

    Joined:
    Feb 9, 2007
    #1
    So I'm taking an intro to programming topics using C++. I have been using Xcode (project type: C++ tool) and TextEdit using the g++ compiler in the terminal concurrently.

    I have yet to receive my textbook and I'm starting to feel a little behind. For our homework this weekend we have to create square by defining the boarder and character type.

    The coding style should look like:
    int main()
    {
    // Define variables border and answer as type char

    // Define variable isSolid as type bool

    // Ask if the square is solid and save to the variable answer

    // based on answer, assign isSolid to true or false

    // Ask second question to get a character border to draw

    // Draw five lines based on the value of isSolid and the border character

    return 0;
    }

    While the output may look like:

    Draw a 5 by 5 square: is it solid? (y/n) y
    Give me the character to draw: 1
    11111
    11111
    11111
    11111
    11111
    Press Q to Exit: q

    Draw a 5 by 5 square: is it solid? (y/n) n
    Give me the character to draw: a
    aaaaa
    a a
    a a
    a a
    aaaaa
    Press Q to Exit: q

    Any help would be appreciated.

    Thanks!
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Use a nested loop. In the inner loop, print one of the character. In the outer loop, print a line break. You could do it without nesting, but if this is a beginning course, the nested loop is probably the intent.

    -Lee
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    Should be to late to claim this as your own (which is why I held off replying after your first responses advice) but a possible solution is presented below for the curious future reader.

    Code:
    #include <iostream>
    
    int main()
    {
    	// Define variables border and answer as type char
    	char	border, answer;	
    	
    	// Define variable isSolid as type bool
    	bool	isSolid;
    
    	while ( true )
    	{
    		while ( true )
    		{
    			// Ask if the square is solid and save to the variable answer
    			std::cout << "\nDraw a 5 by 5 square: is it solid? (y/n) ";
    			std::cin >> answer;
    			
    			// based on answer, assign isSolid to true or false
    			if ( ('y' == answer) || ('Y' == answer) )
    			{
    				isSolid = true;
    				break;
    			}
    			else if ( ('n' == answer) || ('N' == answer) )
    			{
    				isSolid = false;
    				break;
    			}
    		}
    		
    		// Ask second question to get a character border to draw
    		std::cout << "\nGive me the character to draw: ";
    		std::cin >> border;
    		
    		// Draw five lines based on the value of 'isSolid' and the 'border' character
    		for ( int i = 0; i < 5; i++ )
    		{
    #if 1
    			// ... your line solution here ...
    #else
    			// ... because you wouldn't have covered stl strings yet
    			std::string line(5, border);
    
    			if ( isSolid == false )
    			{
    				if ( (i > 0) && (i < 4) )
    				{
    					line.replace(1, 3, 3, ' ');
    				}
    			}
    #endif
    			
    			std::cout << "\n" << line;
    		}
    
    		// Ask third question to quit, else loop
    		std::cout << "\nPress Q to Exit: ";
    		std::cin >> answer;
    
    		if ( ('q' == answer) || ('Q' == answer) )
    		{
    			break;
    		}
    	}
    
    	return 0;
    }
    
     

Share This Page