Thread: Need help on this Rectangle Program

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    6

    Need help on this Rectangle Program

    So this is the rectangle program where program prompts user for height, width, and a char to draw the box/rectangle. Professor wants us to implement two functions where the PrintChar only receives two parameters and does the printing while PrintBox does the calculations... I'm pretty inexperienced and this is what I have so far and im stuck... the videos and old post i've seen isn't clicking in my brain and was hoping someone can finish/explain the looping structure to me... may need to drop this course sadly...

    Need help on this Rectangle Program-hw02-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Copy/paste your code from your text editor, and surround it with [code][/code] tags.
    Make sure to do "copy as text" and/or "paste as text", just in case your IDE likes to include some weird markup along with the program text.

    A barely legible fuzzy images is basically useless.

    We can't read the code.
    We can't test the code.
    There are no line number references to say things like 'you need to do x on line y'
    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.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    6
    So idk why I cant edit the post anymore so...
    Code:
    #include <stdio.h>
    
    
    //Function Prototype
    void PrintBox(int width, int height, char boxChar);
    void PrintChars(int height, char boxChar);
    
    
    //Main Function
    int main(void)
    {
      //Local Variables
      auto int width;
      auto int height;
      auto char boxChar;
      auto int result;
      
      //Prompt user for width inputs and checks for invalid parameters or EOF
      printf("%s", "Please enter a positive box width: ");
      result = scanf("%d", &width);
      if(result != 1)
        {
          puts("Error no width was entered..");
          return 0;
        }
      else if(width > 80 || width < 1)
        {
          puts("Sorry, the width must be between 1 and 80 inclusive...");
          return 0;
        }
    
    
      //Prompt user for height and checks for invalid parameters or EOF
      printf("%s", "Please enter a positive box height: ");
      result = scanf("%d", &height);
      if(result != 1)
        {
          puts("Error no height was entered..");
          return 0;
        }
      else if (height > 80 || height < 1)
        {
          puts("Sorry, the height must be between 1 and 80 inclusive...");
          return 0;
        }
    
    
      //Prompt user for box char and checks for invalid parameters of EOF
      printf("%s", "Please enter the box character: ");
      result = scanf(" %c", &boxChar);
       if(result != 1)
        {
          puts("Error! No character was entered...");
          return 0;
        }
       else if(result = 1)
         {
           PrintBox(width, height, boxChar);
         }  
    }
    
    
    //Functions
    void PrintBox(int width, int height, char boxChar)
    {
    
    
      for(int j = 0; j < height; j++)
        {
          for(int i = 0; i < width; ++i)
        {
          PrintChars(i, boxChar);
        }
        }
     
      //PrintChars(height, boxChar);
    }
    void PrintChars(int boxNumber, char boxChar)
    {
      //Print the width of box
      printf("%c", boxChar);
      
      printf("\n");
      /*for(int i = 1; i <= boxNumber; ++i)
        {
          printf("%c\n", boxChar);
        }
      */
    }
    P.S. Ty Salem

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    The first problem to tackle would be working out what parameters PrintChars should receive.

    For the moment, forget about the two functions and just code in main the following.
    Code:
    for ( int h = 0 ; h < height ; h++ ) {
      for ( int w = 0 ; w < width ; w++ ) {
    	putchar(boxChar);
      }
      putchar('\n');
    }
    Run it at make sure it works.

    The inner loop will become your printChars function. You can see that the variables needed would be width and boxChar.

    So copy the inner loop to the function, and edit your main loop to be
    Code:
    for ( int h = 0 ; h < height ; h++ ) {
      printChars(width, boxChar);
      putchar('\n');
    }
    Test again to make sure it's as it was before.


    Then you repeat the same process again, moving the outer loop to PrintBox, and main just becomes a single function call.



    Oh, and watch your use of = vs ==.
    Code:
    foo.c: In function ‘main’:
    
    foo.c:56:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    
       56 |    else if(result = 1)
    
          |            ^~~~~~
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program to create rectangle
    By Mark Cohen in forum C Programming
    Replies: 28
    Last Post: 07-19-2013, 10:54 AM
  2. Object-oriented Rectangle program
    By LearnOnTheFly in forum C++ Programming
    Replies: 24
    Last Post: 02-18-2013, 10:38 PM
  3. Rectangle
    By juice in forum C Programming
    Replies: 10
    Last Post: 10-29-2011, 10:46 PM
  4. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  5. Rectangle
    By Vicious in forum Windows Programming
    Replies: 3
    Last Post: 06-03-2002, 10:15 PM

Tags for this Thread