LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2010, 01:42 PM   #1
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Rep: Reputation: 0
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);
}

Last edited by boilers969; 10-10-2010 at 01:49 PM.
 
Old 10-10-2010, 01:48 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 10-10-2010, 01:50 PM   #3
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
I don't know how to do that.
 
Old 10-10-2010, 01:55 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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..
 
Old 10-10-2010, 02:00 PM   #5
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
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);
}
 
Old 10-10-2010, 02:14 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 10-10-2010, 02:18 PM   #7
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
What would be an easier way to do it? I am required to use at least one for loop, while loop, and switch statement.
 
Old 10-10-2010, 03:03 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-10-2010, 03:06 PM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 10-10-2010, 03:16 PM   #10
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Ok so do I need to do three seperate switch statements?
 
Old 10-10-2010, 07:02 PM   #11
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by boilers969 View Post
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.

Last edited by sag47; 10-10-2010 at 07:03 PM.
 
Old 10-10-2010, 07:04 PM   #12
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
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;
}

Last edited by boilers969; 10-10-2010 at 07:06 PM.
 
Old 10-10-2010, 07:09 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by boilers969 View Post
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.
 
Old 10-10-2010, 07:13 PM   #14
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
I know how to indent. When I copy it over onto here, the indentation goes away.
 
Old 10-10-2010, 07:16 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by boilers969 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What kind of code has shapes in it? theKbStockpiler Programming 2 09-11-2010 01:33 AM
LXer: IntelliJ's Maia shapes up against Eclipse LXer Syndicated Linux News 0 05-29-2009 11:01 PM
Dual monitors, separate X displays; want firefox in both displays bforbes Linux - Desktop 7 10-15-2008 09:26 PM
redirect a program to 2 displays schneidz Programming 0 08-29-2006 11:02 AM
Need benchmark program which displays bus speed. Arodef Linux - Software 2 07-15-2004 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration