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:
|
All times are GMT -5. The time now is 01:52 AM. |