Thread: Printing a Solid Square based on user input

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Printing a Solid Square based on user input

    Hey everyone I'm brand new to c programming and I'm trying to figure out how to draw a solid square using input from the user to give height and length to determine the number of asterisks to print. I'm having trouble figuring out the for loop part of it. Here's what I've written up so far:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    
      int length;
      int height;
    
      printf("Please Enter Length: ");
      scanf("%d", &length);
    
      printf("Please Enter Height: ");
      scanf("%d", &height);
    
      for(/* input for length */)
        printf("*");
    
      for(/* input for height */)
        printf("*");
    
      printf("\n");
    
      return 0;
    }

    The program is supposed to take the input the user entered for height and length and print the square based on those two numbers. So if the user entered "3" and "5" it would show:

    ***
    ***
    ***
    ***
    ***

    If anyone could help me out and guide me to what I should be including in the for loop that would be greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( initializer; truthtest; update )
    initializer: here's where you set the values you are using if you need to before the loop starts
    truthtest: here's what you check to see if you should stop looping or not
    update: here's where you change some values if you need to.

    Like so:
    Code:
    for( x = 0; x < 5; x++ )
    {
        printf( "yay, counting! %d\n", x );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check your notes and textbook, and check the web for tutorials on loops in C. We have one on this site: Cprogramming.com Tutorial: Loops. Then, you'll want to nest those two loops.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thanks I really appreciate the help I'll be sure to check out the tutorial as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  2. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM