C program that displays shapes. Keep getting errors.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include<stdio.h>
int main()
{
char shape=1;
int row;
while(1)
{
printf("Enter shape type (s/h/f):");
scanf("%c",&shape);
printf("Enter shape length: ");
scanf("%d",&row);
if(row<=0)
printf("Shape length cannot be negative. Try again\n");
switch(shape)
{
case 's':
{
int i,row;
for(i=row;i>=1;i--)
printf("*\n");
break;
}
case 'h':
{
int i,j,k,row,m;
m=row;
for(i=1 ; i<=m; i++)
{
for (j=1 ; j>=row; j--)
printf(" ");
{
for(k=1; k<=i; k=k+1)
printf("*");
printf("\n");
}
}
break;
}
case 'f':
{
int i,j,k,row,m;
m=row;
for(i=0;i<m;i++)
{
printf("\n");
for(k=0;k<row;k++)
printf(" ");
for(j=0;j<=i;j++)
printf(" *");
row--;
}
}
}
printf("\n");
break;
}
return 0;
}
Compiler messages:
shape.c: In function âmainâ:
shape.c:27: warning: ârowâ may be used uninitialized in this function
shape.c:35: warning: ârowâ may be used uninitialized in this function
shape.c:52: warning: ârowâ may be used uninitialized in this function
#include<stdio.h>
int main()
{
char shape=1;
int row;
while(1)
{
printf("Enter shape type (s/h/f):");
scanf("%c",&shape);
printf("Enter shape length: ");
scanf("%d",&row);
if(row<=0)
printf("Shape length cannot be negative. Try again\n");
switch(shape)
{
case 's':
{
int i,row;
for(i=row;i>=1;i--)
printf("*\n");
break;
}
case 'h':
{
int i,j,k,row,m;
m=row;
for(i=1 ; i<=m; i++)
{
for (j=1 ; j>=row; j--)
printf(" ");
{
for(k=1; k<=i; k=k+1)
printf("*");
printf("\n");
}
}
break;
}
case 'f':
{
int i,j,k,row,m;
m=row;
for(i=0;i<m;i++)
{
printf("\n");
for(k=0;k<row;k++)
printf(" ");
for(j=0;j<=i;j++)
printf(" *");
row--;
}
}
}
printf("\n");
break;
}
return 0;
}
Compiler messages:
shape.c: In function âmainâ:
shape.c:27: warning: ârowâ may be used uninitialized in this function
shape.c:35: warning: ârowâ may be used uninitialized in this function
shape.c:52: warning: ârowâ may be used uninitialized in this function
You seem to completely ignore what you've been told/asked.
This:
Code:
int i,row;
for(i=row;i>=1;i--)
piece of code is obviously wrong, you have already been told so, you have already been told about your lack of understanding of the very basic programming concept of variables initialization, yet you continue to write the same erroneous code.
Likewise,
Code:
int i,j,k,row,m;
m=row;
is obviously wrong.
Do you understand English ? What does C99 standard say on variables initialization ? What makes you think the variables are initialized ? Why do you even try to run the code if the compiler issues warnings ?
You have declared row right at the start of the main function you don't need to do it again (you have done so inside the case blocks). By declaring a variable with the same identifier in a block of code you hide the original local variable. In this case row had a value in it that the user had entered, but by redeclaring it you are hiding that value.
Its working just fine. The only thing I'm having problems with is terminating the program when the user enters a char other than s,h,f.
Code:
if(shape!=s/h/f)
return 0;
This is not working. It's giving me the following error. Which says they are undeclared.
Code:
shape.c: In function âmainâ:
shape.c:22: error: âsâ undeclared (first use in this function)
shape.c:22: error: (Each undeclared identifier is reported only once
shape.c:22: error: for each function it appears in.)
shape.c:22: error: âhâ undeclared (first use in this function)
shape.c:22: error: âfâ undeclared (first use in this function)
Before you take the time to ask any more questions please go through and read the responses we have already given you more carefully. Be sure to fully understand them.
And stop saying you understand when you don't understand.
You should look up on how to interpret compiler messages as well...
Quote:
shape.c:22: error: âsâ undeclared (first use in this function)
Means that there is a problem in the file shape.c on line 22. Look at line 22 of your code and figure out why it won't work.
My goal is to get the program to terminate if the user enters something other than "s,f,h". If they keep entering those then the program will keep running.
How do you think your if statement is testing that? You've specified that the value of the variable shape be compared with the value of s/h/f, where s, h and f are all variables. You can do division with char variables, because they're treated as integers, I believe. However, I don't think that's what you intend to do..
I don't quite get your explanation. To me it seems like it should work because scanf stores either s,h,f into shape. And then if shape does not equal one of those it will terminate. That's how I read it.
It seems that you're confused. You've written s/h/f and you're testing that this is not equal to the value stored in shape. Do you agree that / is for division? Do you also agree that you're referring to variables s, h and f? Since you haven't put those characters between single quotes, they refer to variables and not character literals 's', 'h' and 'f'. You haven't declared variables s, h and f, hence the compiler errors. Also, you probably want to look up the or operator in C..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.