Create Custom Shapes with User Input in C Programming

In summary, the conversation discusses the process of writing a program that takes user input to create certain shapes. The user can input 's' for a solid square, 'b' for a hollow box, or 't' for a triangle. They also choose the number of rows and the character to use for drawing the shape. The code asks for the necessary inputs and then proceeds to create the shape. The final code uses for loops and if statements to create the desired shape based on user input. The conversation also includes a request for tips on improving the code and a question about simplifying the use of multiple characters in a printf statement.
  • #1
jaydnul
558
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
  • #2
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.
 
  • #3
nvm, I got it after a little more tinkering. Just thought I'd post cause i had been stuck for a while. Thanks anyways!
 
  • #4
Would you show us what you have? We might be able to offer you some tips on improving your code.
 
  • #5
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?
 

1. What is C programming?

C programming is a high-level, general-purpose programming language that was originally developed in the 1970s. It is widely used for creating system software, embedded systems, and applications that require high performance and efficiency.

2. What are the basic concepts of C programming?

The basic concepts of C programming include variables, data types, operators, control structures, and functions. These concepts are used to write instructions that the computer can understand and execute to perform specific tasks.

3. What are the benefits of learning C programming?

Learning C programming can open up many opportunities in the field of computer science and software development. It can improve problem-solving skills, enhance understanding of computer architecture, and provide a strong foundation for learning other programming languages.

4. What are some common applications of C programming?

C programming is commonly used for developing operating systems, device drivers, game engines, and other system software. It is also used for creating applications in areas such as finance, engineering, and scientific computing.

5. What are some resources for learning C programming?

There are many online tutorials, books, and courses available for learning C programming. Some popular resources include the C programming language book by Brian Kernighan and Dennis Ritchie, online courses on websites like Coursera and edX, and programming communities like Stack Overflow.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
880
  • Engineering and Comp Sci Homework Help
Replies
3
Views
668
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
925
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top