LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C program that displays shapes. Keep getting errors. (https://www.linuxquestions.org/questions/programming-9/c-program-that-displays-shapes-keep-getting-errors-837239/)

boilers969 10-10-2010 01:42 PM

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);
}

Nylex 10-10-2010 01:48 PM

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.

boilers969 10-10-2010 01:50 PM

I don't know how to do that.

Nylex 10-10-2010 01:55 PM

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..

boilers969 10-10-2010 02:00 PM

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>

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);
}


Nylex 10-10-2010 02:14 PM

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.

boilers969 10-10-2010 02:18 PM

What would be an easier way to do it? I am required to use at least one for loop, while loop, and switch statement.

Tinkster 10-10-2010 03:03 PM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.

Nylex 10-10-2010 03:06 PM

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.

boilers969 10-10-2010 03:16 PM

Ok so do I need to do three seperate switch statements?

sag47 10-10-2010 07:02 PM

Quote:

Originally Posted by boilers969 (Post 4123104)
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.

boilers969 10-10-2010 07:04 PM

So i attempted it with 3 separate switch statements.

Code:

#include<stdio.h>

int main()
{
while(1){
char shape;
char s,h,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){
switch(s)
{
case 1:
{int i,row;
for(i=row;i>=1;i--)
printf("*\n");
break;}}}
for(int h=2){
switch(h)
{
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;}}}
for(int f=3)
switch(f)
{
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;}
}}
return 0;
}


Sergei Steshenko 10-10-2010 07:09 PM

Quote:

Originally Posted by boilers969 (Post 4123235)
So i attempted it with 3 separate switch statements.

Code:

#include<stdio.h>

int main()
{
while(1){
char shape;
char s,h,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){
switch(s)
{
case 1:
{int i,row;
for(i=row;i>=1;i--)
printf("*\n");
break;}}}
for(int h=2){
switch(h)
{
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;}}}
for(int f=3)
switch(f)
{
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;}
}}
return 0;
}


Your code is unreadable because it is not indented.

This:

Code:

{int i,row;
for(i=row;i>=1;i--)

just proves that you do not understand a very basic concept of variables initialization. Read the C99 standard first.

boilers969 10-10-2010 07:13 PM

I know how to indent. When I copy it over onto here, the indentation goes away.

Sergei Steshenko 10-10-2010 07:16 PM

Quote:

Originally Posted by boilers969 (Post 4123247)
I know how to indent. When I copy it over onto here, the indentation goes away.

No it doesn't - at least when I copy-paste here.


All times are GMT -5. The time now is 01:52 AM.