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 Please help! Thank you! #include<stdio.h> int main() { while(1>0){ char shape; char s,h,f; shape='s'; shape='h'; shape='f'; 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"); } for(int s=1,int h=2,int f=3){ switch(s,h,f) { case 1: {int i,row; for(i=row;i>=1;i--) printf("*\n"); break;} case 2: {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 3: {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;} default: break; }} return(0); } |
I've reported this to be moved to Programming, as it's more suitable there.
Can you please post your code inside [code] tags, so as to preserve indentation. Also, it would help if you highlighted the problematic lines. |
I don't know how to do that.
|
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 Please help! Thank you! Code:
#include<stdio.h> |
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. |
What would be an easier way to do it? I am required to use at least one for loop, while loop, and switch statement.
|
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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. Again, it would help if you put indentation in. |
Ok so do I need to do three seperate switch statements?
|
Quote:
That would make our "job" easier, as we all donate our time. |
So i attempted it with 3 separate switch statements.
Code:
#include<stdio.h> |
Quote:
This: Code:
{int i,row; |
I know how to indent. When I copy it over onto here, the indentation goes away.
|
Quote:
|
Well for me it doesn't. I'm just asking for help.
|
Quote:
|
Well for some reason it doesn't work for me. I'm just asking for help.
|
I'm not worried about that. I just want my program to compile.
|
Quote:
|
I did. When you first posted it.
|
Quote:
Quote:
Code:
for (j=1 ; j>=row; j--) Code:
for(k=1; k<=i; k=k+1) Code:
#include<stdio.h> Code:
}} As for the rest of your code I think another member would better be able to help you using the indented version. I don't know how to program C. At least not outside your basic hello world or embedded circuits. |
Quote:
Code:
shape='s'; Code:
for(int s=1,int h=2,int f=3) Code:
switch(s,h,f) Code:
{int i,row; |
Thanks sag47. Now if only I could figure out what's wrong.
|
Quote:
|
Haha that's obviously why I'm on here. And your c99 standard isn't helping me.
|
Since you're on Windows hopefully you're not using Notepad. Better to use something like Notepad++ which is what I formatted your code in around 15 seconds.
|
Quote:
|
I'm on windows but I'm using Putty.
Sergei, i was hoping for help directly, not getting refered to a book. |
Quote:
Instead of reading and then asking you insist on being lazy and exploiting others. I.e. you are fiercely resiting reading and comprehending. In real life standards are sometimes 700 pages long and maybe longer. Nobody will hold your hand begging you to read. |
I've spent over 20 hours on this code trying to figure things out. I think you would be sick of it too.
|
Quote:
|
What parts will help me? Assignments, commas, and switch statement? And where are the examples?
|
The age old *nix phrase RTFM comes to mind :). If you really spent that long at troubleshooting it then here's some tips so you don't commit suicide (waiver, I'm not implying you should commit suicide ;)).
#1 Find a spot on the wall that is far away. Every 10 minutes or so take your eyes away from the screen and look at that far away spot. Look at it until at least your eyes sharply focus on the object or picture you picked. Otherwise your eyesight will start to fade. #2 Sometimes enough is enough. You have to force yourself to put the code down. I've troubleshooted code for over 4 hours before and then said heck with it. I can't find it right now. I left it for a couple days or at least a day. Then when I came back I found the error within seconds and continued on code. It really does help to move away from it and come back later so you're not wasting 16 of your 20 hours. Other than that the only thing we, as in LQ members, can do is to help you here and give you hints there; refer you to reference material; etc. It is a habit of mine and others to not just give you the answer. We give you where the answers can be found so that the user may better themselves by reading it, thus becoming a better programmer, and becoming a better human being by reinforcing troubleshooting skills by causing them to think. I say that because Sergei Steshenko is not f**king with you. He's trying to show you where you can find the answer so you can better yourself. Telling you the answer in cases like this almost never helps. Another thing you should consider is time. Members at LQ donate their time and it is not always prudent for us to read the documentation for you and then give you the verbatim answer. So we show you where it can be found. I just wanted to explain that before you exploded into a rage because I can see you're frustrated and we're doing our best to guide you. |
At this point I'm just trying to get the program to compile.
These are the errors. The ones I don't understand are the "expected expression before" and "expected identifier" ones. shape2.c: In function âmainâ: shape2.c:16: error: expected expression before â)â token shape2.c:18: error: âshapeâ undeclared (first use in this function) shape2.c:18: error: (Each undeclared identifier is reported only once shape2.c:18: error: for each function it appears in.) shape2.c:29: error: expected â,â or â;â before â)â token shape2.c:51: error: expected expression before âforâ shape2.c:70: error: expected expression before âbreakâ shape2.c:71: error: expected expression before â}â token shape2.c: At top level: shape2.c:72: error: expected identifier or â(â before â}â token shape2.c:72: error: expected identifier or â(â before â}â token shape2.c:73: error: expected identifier or â(â before âreturnâ shape2.c:74: error: expected identifier or â(â before â}â token |
Please post an updated version of your code so we may look at it. Just seeing the errors doesn't help in this scenario.
I assume "before â}â token" was because you had one too many } brackets and that return 0 statement was outside of your main function. Based on that assumption one could assume that "before â)â token" indicates that you have a loop, conditional, and/or switch statement with one too many ) parenthesis. Try looking harder at that. If you're using vim on your remote machine then you may want to enable some options such as syntax highlighting, autoindenting, other stuff with the following vim commands. Code:
:set showmode Code:
:set ruler! |
Quote:
Can you break the code down to smaller sub-problems. Tackle each sub-problem one at a time rather than trying to solve everything at once. I don't know what your background is, are you learning by yourself or at school / college? Do you have a text book, are you using any online tutorials? |
This is my updated version.
Code:
#include<stdio.h> |
Quote:
|
check the syntax for a for loop.
This is what you have: for(int s=1;) Ask yourself: What should a for loop look like? How does the expected differ from the one coded here? How should it be changed? |
graemef
I'm in college and they don't offer text for this class. |
A quick look at your code. You have:
74 lines of code one while loop ten for loops (with a maximum depth of 3 levels of nesting) one if conditional three switch conditionals Without looking further that tells me that you need to go back to the design and think what it is you want to do. A total of eleven loops in a single function indicates a fundamental problem with your design. Now looking at your code you have a while loop which will never be broken out of so all the code that follows will never be executed so is redundant until you sort out the while loop issue. So a gentle piece of advice remove all the other code and use that while loop as your starting point and then think about the design. |
Quote:
|
Have you studied functions yet?
This problem is crying out for them, but if not don't worry it can be coded in a single function? |
My goal is to have the program keep running and never terminate unless a user enters a char other than s,h,f.
|
Quote:
Code:
#include<stdio.h> |
Would something like this work?
Code:
#include<stdio.h> |
You had it before:
Code:
#include<stdio.h> |
First I would restructure it a little. Check that a valid option has been entered s, h or f before asking for the length.
Second the if conditional needs to be broken down into three parts: is it a 's' or is it a 'h' or is it a 'f'. Often it is easier to consider the positive outcome (was a valid value entered) rather that the negative outcome (was an invalid value entered). The negative outcome will be part of the else statement. To exit from the program you can use the return statement. |
So looking at the next goal. I want to output something for what they input. So is it alright to use 3 different switch statements?
|
All times are GMT -5. The time now is 11:29 AM. |