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.

boilers969 10-10-2010 07:18 PM

Well for me it doesn't. I'm just asking for help.

Sergei Steshenko 10-10-2010 07:21 PM

Quote:

Originally Posted by boilers969 (Post 4123258)
Well for me it doesn't. I'm just asking for help.

First read the C99 standard, then we'll deal with the copy->paste->indent problem.

boilers969 10-10-2010 07:21 PM

Well for some reason it doesn't work for me. I'm just asking for help.

boilers969 10-10-2010 07:22 PM

I'm not worried about that. I just want my program to compile.

Sergei Steshenko 10-10-2010 07:22 PM

Quote:

Originally Posted by boilers969 (Post 4123262)
Well for some reason it doesn't work for me. I'm just asking for help.

Read the C99 standard first.

boilers969 10-10-2010 07:24 PM

I did. When you first posted it.

sag47 10-10-2010 07:27 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;
}


You're attacking this all wrong. First you need to indent like Nylex said.
Quote:

Originally Posted by Nylex
Again, it would help if you put indentation in.

Another issue that I have with you doing is your fancy for loops not using curly brackets {}. Although it is not programmatically incorrect it is a bad habit just like not indenting. The reason is you end up with code like this (directly pulled from your code):
Code:

for (j=1 ; j>=row; j--)
    printf(" ");
{
    for(k=1; k<=i; k=k+1)
        printf("*");
    printf("\n");
}

So in the future you should write your for loops like so which will make your code more accurate and easier to read if you're on a project with more than one person programming:
Code:

for(k=1; k<=i; k=k+1)
{
    printf("*");
}

That problem occurs at line 41 of your code which I will give you back fully indented. Make indenting a habit and your troubleshooting will get dramatically easier and you'll find that you can easily write accurate code the first time.

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

Notice at the end:
Code:

}}
return 0;
}

That is not inside of your main function at all... indenting would have avoided that.

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.

Sergei Steshenko 10-10-2010 07:30 PM

Quote:

Originally Posted by boilers969 (Post 4123268)
I did. When you first posted it.

If so, justify according to the standard the following pieces of your code:


Code:

shape='s';
shape='h';
shape='f';

,
Code:

for(int s=1,int h=2,int f=3)
,
Code:

switch(s,h,f)
,
Code:

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

.

boilers969 10-10-2010 07:30 PM

Thanks sag47. Now if only I could figure out what's wrong.

Sergei Steshenko 10-10-2010 07:32 PM

Quote:

Originally Posted by boilers969 (Post 4123275)
Thanks sag47. Now if only I could figure out what's wrong.

Justify every line of your code by paragraphs of C99 standard. Start from the ones mentioned in http://www.linuxquestions.org/questi...ml#post4123274 .

boilers969 10-10-2010 07:35 PM

Haha that's obviously why I'm on here. And your c99 standard isn't helping me.

sag47 10-10-2010 07:38 PM

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.

Sergei Steshenko 10-10-2010 07:48 PM

Quote:

Originally Posted by boilers969 (Post 4123279)
... your c99 standard isn't helping me.

Then programming is a wrong thing for you to do. Your code shows that you either never read the standard or didn't bother to understand.

boilers969 10-10-2010 07:55 PM

I'm on windows but I'm using Putty.
Sergei, i was hoping for help directly, not getting refered to a book.

Sergei Steshenko 10-10-2010 08:01 PM

Quote:

Originally Posted by boilers969 (Post 4123297)
...
Sergei, i was hoping for help directly, not getting refered to a book.

And that's the problem. Various posters including myself pointed you to the exact places in your code which are hopelessly wrong. And you were given sources of info to get knowledge from.

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.

boilers969 10-10-2010 08:06 PM

I've spent over 20 hours on this code trying to figure things out. I think you would be sick of it too.

Sergei Steshenko 10-10-2010 08:10 PM

Quote:

Originally Posted by boilers969 (Post 4123307)
I've spent over 20 hours on this code trying to figure things out. I think you would be sick of it too.

You should have spent time reading the C99 standard. Again, the code shows you do not understand very basic stuff. So, read the C99 standard.

boilers969 10-10-2010 08:11 PM

What parts will help me? Assignments, commas, and switch statement? And where are the examples?

sag47 10-10-2010 08:20 PM

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.

boilers969 10-10-2010 08:27 PM

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

sag47 10-10-2010 08:41 PM

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
:set ruler
:set number
:set background=dark
:syntax on
:set autoindent

Turn them off like
Code:

:set ruler!
:syntax off
:set background=light


graemef 10-10-2010 08:43 PM

Quote:

Originally Posted by boilers969 (Post 4123297)
I'm on windows but I'm using Putty.

As I understand it Putty is a terminal emulator for ssh communication. This is not the best tool for source code editing. Can you find an editor (such as notepad++ - previously mentioned) you may then find that the indentation problem that you are having when posting here goes away. Secondly, as a programmer you need to know what tools you have available and use the right ones. You can't be a carpenter if the only tool you use is a hammer.

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?

boilers969 10-10-2010 08:46 PM

This is my updated version.

Code:

#include<stdio.h>

int main()
{
  while(1)
        {
        char shape=1;
        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(shape)
  {
        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;
}


sag47 10-10-2010 08:50 PM

Quote:

Originally Posted by graemef (Post 4123331)
As I understand it Putty is a terminal emulator for ssh communication. This is not the best tool for source code editing.

vim can be used on a remote machine which is just as good as Notepad++.

graemef 10-10-2010 08:52 PM

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?

boilers969 10-10-2010 08:56 PM

graemef
I'm in college and they don't offer text for this class.

graemef 10-10-2010 09:03 PM

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.

graemef 10-10-2010 09:07 PM

Quote:

Originally Posted by boilers969 (Post 4123342)
graemef
I'm in college and they don't offer text for this class.

You might want to look for a good text book. See the sticky at the top of this forum for some ideas.

graemef 10-10-2010 09:09 PM

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?

boilers969 10-10-2010 09:11 PM

My goal is to have the program keep running and never terminate unless a user enters a char other than s,h,f.

graemef 10-10-2010 09:20 PM

Quote:

Originally Posted by boilers969 (Post 4123354)
My goal is to have the program keep running and never terminate unless a user enters a char other than s,h,f.

Okay with that in mind don't worry about drawing the shapes yet but concentrate on the s,h,f terminate functionality. Starting with this segment of code that you already have:

Code:

#include<stdio.h>

int main()
{
  while(1)
  {
      char shape=1;
      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");
  }
}

Can you modify this code to achieve your first goal?

boilers969 10-10-2010 09:37 PM

Would something like this work?

Code:

#include<stdio.h>

int main()
{
  while(1)
  {
      char shape=1;
      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");
      if(shape!=s/h/f)
        then terminate the program(i dont know the command to terminate it)
  }
}


sag47 10-10-2010 10:07 PM

You had it before:

Code:

#include<stdio.h>

int main()
{
  while(1)
  {
      char shape=1;
      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");
      if(shape!=s/h/f)
        return 0;
  }
}


graemef 10-10-2010 10:09 PM

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.

boilers969 10-10-2010 10:10 PM

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.