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.
C program that displays shapes. Keep getting errors.
Here is my program. It asks the user for input and then prints a shape of a certain length. The errors I get when I compile are:
shape.c: In function âmainâ:
shape.c:22: error: expected identifier or â(â before âintâ
shape.c:58: error: expected expression before âreturnâ
shape.c:59: error: expected expression before â}â token
shape.c:59: error: expected expression before â}â token
Put [code] and [/ code] (without the space) around your code. You'll need to paste your code again, so that it has the indentation. As for highlighting the problematic lines, just use colours, or put them in bold (or anything else that will make them stand out). Use the tools above the box where you type your reply..
C program that displays shapes. Keep getting errors.
Here is my program. It asks the user for input and then prints a shape of a certain length. The errors I get when I compile are:
shape.c: In function âmainâ:
shape.c:22: error: expected identifier or â(â before âintâ
shape.c:58: error: expected expression before âreturnâ
shape.c:59: error: expected expression before â}â token
shape.c:59: error: expected expression before â}â token
That for loop is wrong. You've got initialisation parts, but no test and increment or decrement parts. I'm not even sure whether you can even have loops with multiple variables in them.
Also, your return statement should just be "return 0;", i.e. no brackets.
I don't really have time to look at this in detail right now, but there are several things wrong with your code:
1. You have a while loop that appears to be infinite - I don't see a break in there. The loop body appears to end with
printf("Shape length cannot be negative. Try again\n");
In addition, if you do need to loop infinitely (with a break of course), then you only really need while(1), rather than while(1 > 0), since 1 is a synonym for true.
2. You've declared char variables s, h and f and then in your for loop, you want to declare variables with the same name, but of different type. This can be confusing.
3. Your switch statement has 3 variables in it. You can only switch on one.
Ok so do I need to do three seperate switch statements?
Yes, one for each variable. It's good to use google and look up how to write C programming. At the very least look up C functions and concepts like loops. If you feel you've written a bug free program and no amount of google will help you then post your question please.
That would make our "job" easier, as we all donate our time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.