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.
Yes I'm understanding that. But since I can't have any global variables, where can I declare char shape??
shape will be declared within the main function. It will then be available to the switch statement which is in the main. Which will be within your while loop (as you asked about that earlier).
Now might be a good time to post your code as it stands so that we can have a good look at it and help you iron out any problems. Also include any compile errors or warnings that you may have.
So are you saying the while loop will contain the switch statment?
Code:
#include<stdio.h>
int main()
{
while(1)
{
char shape=1;
int row;
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");
}
char shape=1;
shape='s','h','f';
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;
}
Errors:
shape.c: In function âmainâ:
shape.c:25: error: âsâ undeclared (first use in this function)
shape.c:25: error: (Each undeclared identifier is reported only once
shape.c:25: error: for each function it appears in.)
shape.c:33: error: âhâ undeclared (first use in this function)
shape.c:50: error: âfâ undeclared (first use in this function)
shape.c:66: error: break statement not within loop or switch
shape.c: At top level:
shape.c:68: error: expected identifier or â(â before â}â token
shape.c:69: error: expected identifier or â(â before â}â token
shape.c:70: error: expected identifier or â(â before âreturnâ
shape.c:71: error: expected identifier or â(â before â}â token
Yes. while(1) { } is an infinite loop. It will never get to char shape=1. Anything that you do with your code MUST be within the while loop if it is while(1).
So your code should look like
Code:
int main()
{
while(1)
{
...program about everything;
}
}
There shouldn't be anything outside of while(1)below it. You can set variables and other conditions to be used within the while(1)above it however.
Also in order for your program to be successful in any way it has to exit some how. So somewhere in the while(1) you need to have a return statement.
int main()
{
while(1)
{
/* Code to get the value of shape */
switch (shape)
{
case 's':
/* code to handle the shape 's' */
break
/* the remaining case statements */
} /* end of switch */
} /* end of while loop forever */
} /* end of main function */
Note that the value held by shape be it a s h or f must be in single quotes 's', 'h' or 'f'
You don'd need to declare the variable a second time (I guess that you put it there because the compiler complained that it doesn't know about a variable called shape. This is because you declared it in the loop.)
While you're at it, since you're scanning keyboard input by passing a reference it is better to declare shape outside of your while loop as you only need declare it once.
Code:
int main()
{
char shape=1;
/* declare any other variables which have references within the while loop */
while(1)
{
/* Code to get the value of shape */
switch (shape)
{
case 's':
/* code to handle the shape 's' */
break
/* the remaining case statements */
} /* end of switch */
} /* end of while loop forever */
} /* end of main function */
When you refer to s you are referring to the character value s. Not a variable that could be any value. but the specific value s. These are called character literals, and in C you enclose them in single quotes. Hence you would have case 's':notcase s: Okay.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.