Create Custom Shapes with User Input in C Programming

AI Thread Summary
The discussion focuses on creating a C program that generates shapes based on user input, including solid squares, hollow boxes, and triangles. The user specifies the shape type, number of rows, and the character used for drawing. The original poster struggled with dynamically adjusting the number of rows and sought help for the implementation. After some tinkering, they successfully wrote code to handle the shapes, although they expressed a desire to simplify it further. The conversation also touches on optimizing the code, particularly regarding repetitive character printing in the `printf` function.
jaydnul
Messages
558
Reaction score
15
I have to write a program that will take user input to make certain shapes. If the user inputs 's', its a solid square, 'b', its a hollow box, 't' its a triangle. They also choose how many rows it is and what the shape is made of. Example

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

this would be a user input of 's', 3 rows, and the * symbol.
Now the part i can't figure out is how to specify the rows. I would have just used if statements and make a shape out a bunch of %c but i can't figure out how to make it more or less rows by user input. This is my code so far:

#include <stdio.h>
#include <stdlib.h>
void main()
{
char a,c;
int b;

printf("Enter \"s\" for a square\nEnter \"b\" for a box\nEnter \"t\" for a triangle\n");
scanf("%c",&a);
system("cls");
if (a=='s')
printf("Square\n--------------------…
if (a=='b')
printf("Box\n--------------------\n"…
if (a=='t')
printf("Triangle\n------------------…

printf("Enter the number of rows\n");
scanf("%d",&b);
system("cls");
if (a=='s')
printf("Square, %d rows\n--------------------\n",b);
if (a=='b')
printf("Box, %d rows\n--------------------\n",b);
if (a=='t')
printf("Triangle, %d rows\n--------------------\n",b);

printf("Enter the character to use for drawing\n");
scanf(" %c",&c);
system("cls");
if (a=='s')
printf("Square, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…
if (a=='b')
printf("Box, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…
if (a=='t')
printf("Triangle, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…

/*Making the shape*/




}

And obviously i don't have anything under "making the shape". So far, my code asks for the 2 characters and 1 variable i need from the user but i don't know how to go about the rest.

Any help? thanks in advance
 
Physics news on Phys.org
As an option to iteratively producing one line of a shape at a time, you could allocate a matrix of characters, and fill in the matrix with spaces and the user selected character, then output the matrix one line at a time.
 
nvm, I got it after a little more tinkering. Just thought I'd post cause i had been stuck for a while. Thanks anyways!
 
Would you show us what you have? We might be able to offer you some tips on improving your code.
 
This is what i ended up with:

#include <stdio.h>
#include <stdlib.h>
void main()
{
char a,c;
int b,i,d,space,k=0;

printf("Enter \"s\" for a square\nEnter \"b\" for a box\nEnter \"t\" for a triangle\n");
scanf("%c",&a);
system("cls");
if (a=='s')
printf("Square\n--------------------\n");
if (a=='b')
printf("Box\n--------------------\n");
if (a=='t')
printf("Triangle\n--------------------\n");

printf("Enter the number of rows\n");
scanf("%d",&b);
system("cls");
if (a=='s')
printf("Square, %d rows\n--------------------\n",b);
if (a=='b')
printf("Box, %d rows\n--------------------\n",b);
if (a=='t')
printf("Triangle, %d rows\n--------------------\n",b);

printf("Enter the character to use for drawing\n");
scanf(" %c",&c);
system("cls");
if (a=='s')
printf("Square, %d rows, made of \"%c\" characters\n--------------------\n",b,c);
if (a=='b')
printf("Box, %d rows, made of \"%c\" characters\n--------------------\n",b,c);
if (a=='t')
printf("Triangle, %d rows, made of \"%c\" characters\n--------------------\n",b,c);

/*Making the shapes*/

/*Square*/
if (a=='s')
for (i=0;i<b;i++)
printf("%c%c%c%c%c\n",c,c,c,c,c);

/*Box*/
d=b-2;
if (a=='b')
printf("%c%c%c%c%c\n",c,c,c,c,c);
if (a=='b')
for (i=0;i<d;i++)
printf("%c %c\n",c,c);
if (a=='b')
printf("%c%c%c%c%c\n",c,c,c,c,c);

/*Triangle*/
if (a=='t')
for(i=1;i<=b;++i)
{
for(space=1;space<=b-i;++space)
{
printf(" ");
}
while(k!=2*i-1)
{
printf("%c ",c);
++k;
}
k=0;
printf("\n");
}
}

If you have any tips on making it simpler, feel free to post em cause if feel like that code is way long for what it needs to do (although it does do it). One question i do have is when i have something like printf("%c%c%c%c",c,c,c,c), is there a way to just have one ,c rather than one to match each %c in the string?
 

Similar threads

Replies
12
Views
2K
Replies
3
Views
1K
Replies
1
Views
10K
Replies
3
Views
1K
Replies
9
Views
4K
Replies
4
Views
1K
Replies
21
Views
2K
Replies
1
Views
2K
Back
Top