I am new to this C thing and am triny very hard to understand but am having a little problem getting thing to work. I have a project that I have been working on and cannot seem to get it to work.
This is what I have so far and for the life of me can not seem to get it to work. I know I am missing something. When it is compiled it is supposed to ask the question How long would it be on a side? and the user enters a number and then after the number is entered a square would be formed usings asteriks. So, can anyone help me??? Please...:lol:

#include <stdio.h>
main()
{
         int length;
         int width;
         int count = 0;
         printf("How long would it be on a side?\n");
         scanf("%d*%d",&length,&width);
         printf("\n");

       while (count <= length*width){
         printf("*");
         count++;
       }
}

"square.c" 27 lines, 313 characters

1. First of all main returns int so your prototype should be int main (void) with a return 0 at the end of the program.

2. You wanted a square of * but you ask the user two parameters width and height , so do you really want a square or a rectangle.

3. You are using while loop the wrong way... use nested for loops to achieve the same purpose.

for( i = 0; i < height; ++i )
{
    for( j = 0; j < width; ++j )
    {
         putchar( '*' ) ;
    }
}

// or to modify what you have done..
count = 1 ;
while( count <= (height * width) )
{
   if( count % width == 0 )
      putchar( '\n' ) ;
   putchar( '*' ) ;
   ++count ;
}

Hope it helped , bye.

I tried this but am confused and received errors. I am trying to get an out put that look like this when complied and the user input a number:

say the user inputs 15 for the number; then there woud be a square made up of * 15 accross top and bottom and 15 down each side.

I tried this but am confused and received errors.

What errors? We can't help if we don't know what the errors are nor what the new program looks like.

Remember, to output *s on the next line you have to output a '\n' at the end of the line you're on.

I tried this but am confused and received errors. I am trying to get an out put that look like this when complied and the user input a number:

say the user inputs 15 for the number; then there woud be a square made up of * 15 accross top and bottom and 15 down each side.

The code snippet I posted is perfectly alright and just requires data from the main program or the driver program to print the result on the screen. Are you sure you have written the prog as I have pointed out.

Post the code which you have attempted and then maybe i can help you out.

Okay I was able to get it to run with no errors but still am unable to get the output I need. I enter the following code:

#include <stdio.h>
main()
{
int length;
int width;
int count = 1;
 
printf("How long would it be on a side?\n");
scanf("%d*%d",&length,&width);
printf("\n");
 
while ( count ,= (length *width)){
printf("*\n");
++count;
}
 
while (count % width == 0)}
printf("*\n");
++count;
}
}

and I complie and when asket the question get the answer but need to get a square of asteriks using the number(s) entered by the user for example:

How long would it be on a side? 5
*****
* *
* *
* *
* *
*****

The return be a square of this nature.

The code which you posted doesnt even compile. Try to post error free code which will help us find you a solution quickly. Also the changes which i had asked you to made werent totally made. Why keep two while loops i dont know ?

Here is the slightly updated code. Compare it with what you had posted and see where you had made mistakes.

One more thing:

1. main returns int so dont use main() use int main( ) 2. Try to keep the qualifiers list of scanf clean otherwise you are in for a lot of surprises. If possible dont use spaces in scanf while accepting input unless necessary.

#include <stdio.h>
int main()
{
    int height = 0; // if possible initialize variables while declaering them to avoid subtle bugs.
    int width = 0;
    int count = 0;

    printf( "Enter the width of rectange: " ) ;
    scanf("%d", &width);
    printf( "Enter the height of rectange: " ) ;
    scanf("%d", &height);
    putchar( '\n' ) ;
    while( count < (height * width) )
    {
       if( count % width == 0 )
          putchar( '\n' ) ;
       putchar( '*' ) ;
       ++count ;
    }
    putchar( '\n' ) ;
    return 0 ;
}

Hope it helped, bye.

You have helped me a great deal and I am foreve greatful, but I need to have the inside of the square/rectangle empty. Is that possible?

Yes very much possible, but you just have to modify the existing a bit.

But I aint attempting it for you. For you to be a good programer, you have to try it yourself and if you get stuck i will help you out.

just wanted to make sure its possible, as i said before, i am jsust learning this and this is a new field for me at my age. Been in the accounting field for 40+ years and now taking on computers which I love. will keep you posted on how I make out. Again thank you for your help....

I went back to square one and started over some what and I have been able to complete most of the project and get a completed square, but now I am still unable to get the middle with no asteriks. Can you please push me into the right direction, I know I need to use an if statement, but as to how I am confused. I have been trying to understand them, but they confuse me. I just started taking this class back in September and am really tring to understand it. Can you tell me what you think of my new program below?
Thank you
Barb

#include <stdio.h>
#define Limit >=4 <=20
 int main()
{
        int count1 = 1;
        int count2 = 1;
        int limit;
 
        printf("How long should the side be?\n ");
        scanf("%d",&limit);
 
        while (count1 <= limit)
        {
                while (count2 <= limit)
                {
                        printf("*");
                        count2++;
                }
                count2 = 1;
                printf("\n");
                count1++;
        }
}

Okay now to achieve the hollow effect we observe a few things:

1. The box will have complete lines of * only for the first and the last rows.

* * * * * * <=  First row complete line
*            *
*            *
*            *
*            *
* * * * * * <=  Last row complete line

2. So in your program, both the while loops will virtually remain the same except for the fact that the logic within the second while loop should be updated to take care of teh condition I laid in front of you.

Something like:

while( i <= limit )
{
    while( j <= limit )
    {
         if( first and last row )
         {
              // handle it in a different way
         }
          else
         {
             // we need only the first and last *
             // handle differently for the rem. rows
         }
     }
}

Try it out yourself and it will definately work.

PS: Please enclose the code you post in [code] // your entire code [/code] tags .

Okay, so where you stated // handle it in a different way I ned to put the code for printing the asteriks for hte first and last rows. ans then handle differently the remaining rows the code to be able to rpint an astrisk and spaces then an asterik at the other end.am i understanding you correctly?

Okay, please excuse and explain code tage, I am still learning this programming. I want to be able to do things correctly.

Barb

This article has been dead for over six months. Start a new discussion instead.