Create Custom Shapes with User Input in C Programming

Click For Summary

Discussion Overview

The discussion revolves around creating a C program that generates custom shapes based on user input. Participants explore how to implement user-defined shapes such as squares, boxes, and triangles, including the ability to specify the number of rows and the character used for drawing. The focus is primarily on coding techniques and logic for shape generation.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes their initial approach to creating shapes using user input and expresses difficulty in specifying the number of rows dynamically.
  • Another participant suggests allocating a matrix of characters to fill in with spaces and the user-selected character, then outputting the matrix line by line as an alternative method.
  • A participant later reports that they resolved their issue after further tinkering with their code.
  • Another participant requests to see the updated code to provide tips for improvement.
  • The original poster shares their revised code, which includes implementations for generating squares, boxes, and triangles based on user input.
  • The original poster asks for suggestions on simplifying their code, particularly regarding the use of multiple format specifiers in printf statements.

Areas of Agreement / Disagreement

Participants generally agree on the approach to creating shapes but do not reach a consensus on the best coding practices or simplifications. The discussion remains open to suggestions for improvement.

Contextual Notes

The discussion includes various coding techniques and logic implementations, but some participants express uncertainty about the efficiency and simplicity of their code. There are also unresolved questions about optimizing printf usage.

Who May Find This Useful

Readers interested in C programming, particularly those looking to enhance their skills in user input handling and shape generation, may find this discussion beneficial.

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 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K