Output an asterisk rectangle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashahayes
    New Member
    • Mar 2010
    • 13

    Output an asterisk rectangle

    This program prints the outline of a rectangle with 6 asterisks
    across and 8 asterisks down.
    Input: None
    Output: Prints the outline of the rectangle.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {								
       const int NUM_ACROSS = 6; 	
       const int NUM_DOWN = 8;		
       int row;	
       int column;	
    
       for (row = 0; row <= NUM_ACROSS; row++)
       for (column = 0; column <= NUM_DOWN; column++)
          
            if (row == 0 || NUM_ACROSS == 6)         
                cout << "*";
            
            else if (column == 0 || NUM_DOWN == 8)         
                cout << "*";
             
                else 
                cout << " "; 
         
          cout << endl;
       
       return 0; 
    } // End of main()
    It is suppose to print a rectangle.
    Something like this, but I just keep getting the lines on the top.
    ******
    *
    *
    *
    *
    *
    ******
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    It always prints '*' because NUM_ACROSS==8 is always true. You didn't say what is it doing wrong, though.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You might try adding braces. Your loops are not being compiled they way you think they are. They should be:

      Code:
      for(etc...)
      {
          for(etc...)
          {
               if (etc...)
               {
      
               }
      
          }
      
      
      }

      Comment

      • shashahayes
        New Member
        • Mar 2010
        • 13

        #4
        Now I am just getting the line across the top.

        ************
        Do you have any ideas why I am not getting the down row?
        this is what I have now.
        Code:
        #include <iostream>
        #include <string>
        using namespace std;
        int main()
        {								
           const int NUM_ACROSS = 8; 	
           const int NUM_DOWN = 6;		
           int row;	
           int column;	
        
           for (row = 0; row <= NUM_ACROSS; row++)
           {
           for (column = 0; column <= NUM_DOWN; column++)
           {
                if (row == 0 || NUM_ACROSS == 8)         
                    cout << "*";
        		
                
                else if (column == 0 || NUM_DOWN == 6)         
                    cout << "*";
        		
                 
           // Decide when to print a space instead of an asterisk
                 else 
                    cout << " "; 
        }
              // Figure out where to place this statement that prints a newline
              cout << endl;
           
           return 0; 
        } // End of main()
        
           }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You are not displaying \n characters for your end-of-line.

          There should be a \n at the end opf each line and an endl as the last \n.

          The endl has a built-in buffer flush after it displays a \n. Therefore, you only need this at the end of the entire display. It'll work thoughout but the unneeded buffer flushes will make you code run slower.

          Comment

          • shashahayes
            New Member
            • Mar 2010
            • 13

            #6
            Thank you so much for your help. I am now getting a straight line down instaed of across. Any Ideas on how to fix this?
            *
            *
            *
            *
            *
            This is what I have now.

            #include <iostream>
            #include <string>
            using namespace std;
            int main()
            {
            const int NUM_ACROSS = 8;
            const int NUM_DOWN = 6;
            int row;
            int column;

            for (row = 0; row <= NUM_ACROSS; row++)
            {
            for (column = 0; column <= NUM_DOWN; column++)
            {
            if (row == 0 || row == 8)
            cout << "\n*";

            else if (column == 4 || column == 6)
            cout << "*";


            // Decide when to print a space instead of an asterisk
            else
            cout << "\n ";
            }
            // Figure out where to place this statement that prints a newline
            cout << "\n*" << endl;

            return 0;

            } // End of main()

            }

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              You want to change it back to use the constants, but this time compare the row to the constant:
              if (row == 0 || row == NUM_ACROSS)

              You should only be printing a new line in one of these cases. I suggest print the new line when row == NUM_ACROSS. So make it instead:
              if row is 0 print *
              else if row is NUM_ACROSS print *\n

              ====
              The else case should output just a space, not a newline.
              else
              cout << " ";

              Comment

              • shashahayes
                New Member
                • Mar 2010
                • 13

                #8
                I know I must be over looking something simple. It seems as if the else if statement is doing nothing. All I am getting is a vertical line on the left side. I am at a loss for what to do. Thanks for all your help and if anyone can see what I am doing wrong now please let me know.

                Her is the code

                #include <iostream>
                #include <string>
                using namespace std;
                int main()
                {
                const int NUM_ACROSS = 8;
                const int NUM_DOWN = 6;
                int row;
                int column;

                for (row = 0; row <= NUM_ACROSS; row++)
                {
                for (column = 0; column <= NUM_DOWN; column++)
                {
                if (row == 0 || column == 0 )
                cout << "*\n";

                else if (row == NUM_ACROSS || column == NUM_DOWN )

                "*\n";

                // Decide when to print a space instead of an asterisk
                else
                cout << " ";
                }
                // Figure out where to place this statement that prints a newline
                cout << "*\n" << endl;

                return 0;

                } // End of main()

                }

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  This -
                  else if (row == NUM_ACROSS || column == NUM_DOWN )
                  "*\n";
                  - doesn't output anything, it has no effect. Also, the logic still is wrong - you don't need a newline after each '*' in the first row.

                  Also,
                  return 0;
                  } // End of main()

                  is not 'end of main' but end of the loop, and you never get past the first iteration.

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    Sorry, confused the variables.
                    You should check for end of line first.
                    Code:
                    if(column == NUM_DOWN)
                    cout << "*\n"; 
                    
                    else if (row == 0 || column == 0 || row == NUM_ACROSS)
                    cout << "*";

                    Comment

                    • shashahayes
                      New Member
                      • Mar 2010
                      • 13

                      #11
                      Thanks to all, I finally got to work with all your help !!!

                      Comment

                      • mohitus88
                        New Member
                        • Nov 2013
                        • 1

                        #12
                        this worked fine:

                        #include<iostre am>
                        #include<conio. h>

                        using namespace std;

                        int main(){

                        for(int i=0;i<=8;i++){
                        if(i==0 || i==8){
                        for(int j=0;j<=8;j++)
                        cout<<("*");
                        }
                        else
                        {
                        for(int k=0;k<=8;k++)
                        {
                        if(k==0 || k==8)
                        cout<<"*";
                        else
                        cout<<" ";
                        }
                        }
                        cout<<"\n";
                        }
                        getch();
                        }

                        Comment

                        Working...