Thread: Need guidance on how to make a rectangle be printed with two characters

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    2

    Need guidance on how to make a rectangle be printed with two characters

    Code:
    Hi, I have this one assignment I am working on and I am a bit stuck here
    Code:
    I have to make a rectangle do this with user input:
    
    Side size = 1
    +-+
    | |
    | |
    | |
    | |
    | |
    | |
    +-+
    
    ----------------------------------------------------------------------------
    
    Here's what I have so far
    
    //Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    //Variables declared
    int count1 = 1, limit = 5, count2 = 1;
    char doAgain;
    
    
    cout << "Enter the width for a line of Xs to be drawn on the screen: ";
    cin >> limit;
    
    count1 = 1;
    cout<< '+';
    while(count1 <= limit-2)
    {
    cout << "-";
    count1++; //Loop update condition
    }
    cout<< '+';
    
    cout << endl << endl;
    
    while(count1 <= limit-2)
    {
    cout << " ";
    count1++; //Loop update condition
    }
    cout<< '|';
    
    cout << endl << endl;
    
    while(count2 <= limit-2)
    {
    cout << " ";
    count2++; //Loop update condition
    }
    cout<< '|';
    
    cout << endl << endl;
    
    return 0;
    }
    
    Any guidance would be useful thank you in advance :)
    
    

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    You are using 4 characters: ' ', '+', '-' and '|'.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    2
    Yeah

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Fun View Post
    Yeah
    But you asked how to use 2?!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Why does the first comment of your program say

    Code:
    //Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
    


    and then proceed to use '+', '-', '|' and ' ' characters when the comment says it's only supposed to use 'X'? Makes no sense to me


  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Fun View Post
    Yeah
    Code:
    #include <stdio.h>
    
    static char tbl[] = { 
    28, ' ', 4, 'o', 12, '$', 4, 'o', 1, '\n', 24, ' ', 2, 'o', 24, '$', 1, 'o', 
    1, '\n', 21, ' ', 2, 'o', 30, '$', 1, 'o', 9, ' ', 1, 'o', 1, '$', 3, ' ', 
    2, '$', 1, ' ', 1, 'o', 1, '$', 1, '\n', 5, ' ', 1, 'o', 1, ' ', 1, '$', 
    1, ' ', 2, 'o', 8, ' ', 1, 'o', 36, '$', 1, 'o', 7, ' ', 2, '$', 1, ' ', 
    2, '$', 1, ' ', 2, '$', 1, 'o', 1, '$', 1, '\n', 2, ' ', 2, 'o', 1, ' ', 
    1, '$', 1, ' ', 1, '$', 1, ' ', 1, '"', 1, '$', 6, ' ', 1, 'o', 9, '$', 
    4, ' ', 13, '$', 4, ' ', 9, '$', 1, 'o', 7, ' ', 3, '$', 1, 'o', 2, '$', 
    1, 'o', 1, '$', 1, '\n', 2, ' ', 1, '"', 6, '$', 1, 'o', 1, '$', 5, ' ', 
    1, 'o', 9, '$', 6, ' ', 11, '$', 6, ' ', 10, '$', 1, 'o', 4, ' ', 8, '$', 
    1, '\n', 4, ' ', 7, '$', 4, ' ', 11, '$', 6, ' ', 11, '$', 6, ' ', 23, '$', 
    1, '\n', 4, ' ', 23, '$', 4, ' ', 13, '$', 4, ' ', 14, '$', 2, ' ', 3, '"', 
    3, '$', 1, '\n', 5, ' ', 1, '"', 3, '$', 4, '"', 49, '$', 5, ' ', 1, '"', 
    3, '$', 1, '\n', 6, ' ', 3, '$', 3, ' ', 1, 'o', 50, '$', 5, ' ', 1, '"', 
    3, '$', 1, 'o', 1, '\n', 5, ' ', 1, 'o', 2, '$', 1, '"', 3, ' ', 51, '$', 
    7, ' ', 3, '$', 1, 'o', 1, '\n', 5, ' ', 3, '$', 4, ' ', 45, '$', 1, '"', 
    1, ' ', 1, '"', 6, '$', 5, 'o', 4, '$', 1, 'o', 1, '\n', 4, ' ', 1, 'o', 
    3, '$', 4, 'o', 5, '$', 2, ' ', 37, '$', 3, ' ', 1, 'o', 17, '$', 1, '\n', 
    4, ' ', 8, '$', 1, '"', 4, '$', 3, ' ', 34, '$', 5, ' ', 4, '$', 8, '"', 
    1, '\n', 3, ' ', 4, '"', 7, ' ', 4, '$', 4, ' ', 1, '"', 28, '$', 1, '"', 
    6, ' ', 1, 'o', 3, '$', 1, '\n', 14, ' ', 1, '"', 3, '$', 1, 'o', 5, ' ', 
    3, '"', 18, '$', 1, '"', 2, '$', 1, '"', 9, ' ', 3, '$', 1, '\n', 16, ' ', 
    3, '$', 1, 'o', 10, ' ', 1, '"', 2, '$', 2, '"', 6, '$', 4, '"', 11, ' ', 
    1, 'o', 3, '$', 1, '\n', 17, ' ', 4, '$', 1, 'o', 32, ' ', 1, 'o', 3, '$', 
    1, '"', 1, '\n', 18, ' ', 1, '"', 4, '$', 1, 'o', 6, ' ', 1, 'o', 6, '$', 
    1, 'o', 1, '"', 4, '$', 1, 'o', 8, ' ', 1, 'o', 4, '$', 1, '\n', 20, ' ', 
    1, '"', 5, '$', 2, 'o', 5, ' ', 2, '"', 4, '$', 1, 'o', 5, '$', 1, 'o', 
    3, ' ', 1, 'o', 4, '$', 2, '"', 1, '\n', 23, ' ', 2, '"', 5, '$', 4, 'o', 
    2, ' ', 1, '"', 3, '$', 1, 'o', 9, '$', 3, '"', 1, '\n', 26, ' ', 2, '"', 
    7, '$', 2, 'o', 1, ' ', 10, '$', 1, '\n', 34, ' ', 4, '"', 11, '$', 1, '\n', 
    38, ' ', 12, '$', 1, '\n', 39, ' ', 10, '$', 1, '"', 1, '\n', 40, ' ', 1, '"', 
    3, '$', 4, '"', 1, '\n',  0 };
    
    int main( void )
    {
      char *p = tbl;
      char c, len;
    
      while ( *p )
      {
        len = *p++;
        c = *p++;
    
        while ( len-- ) putchar( c );
      }
    }
    Hehehe

    PS: Change main() to the code below if it doen't work on Windows:
    Code:
    int main( void )
    {
      char *p = tbl;
      char c, len;
    
      while ( *p )
      {
        len = *p++;
        c = *p++;
    
        while ( len-- ) 
        {
    #ifdef __WIN32
          if ( c == '\n' )
          {
            fputs( "\r\n", stdout );
            continue;
          }
    #endif
    
          putchar( c );
        }
      }
    }
    Last edited by flp1969; 10-12-2019 at 06:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2009, 02:32 AM
  2. Numbers printed with characters
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 05-08-2008, 07:07 AM
  3. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  4. How to make a Rectangle
    By kumar14878 in forum C Programming
    Replies: 4
    Last Post: 02-02-2006, 08:14 AM
  5. Funny characters being printed
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-17-2005, 09:02 AM

Tags for this Thread