Create Custom Shapes with User Input in C Programming

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
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
 
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!
 
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?