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-11-2010 04:40 PM

Except now it is just terminating every time.
Here is my code:

Code:

#include<stdio.h>

int main()
{
char shape=1,enter;
int row;
 while(1)
        {
        printf("Enter shape type (s/h/f):");                       
        scanf("%c%c",&shape,&enter);                               
        printf("Enter shape length: ");                             
        scanf("%d%c",&row,&enter);                                 
        if(row<0)                                                   
        printf("Shape length cannot be negative. Try again\n");     
        if(shape!='s'||'h'||'f')
                return 0;
        switch(shape)
        {
        case 's':
          {
          int i;
          for(i=row;i>=1;i--)
                printf("*\n");
          break;
          }

        case 'h':
          {
          int i,j,k,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 'f':
        {
                int i,j,k,m;
                m=row;
                for(i=0;i<m;i++)
                {
                  for(k=0;k<row;k++)
                        printf(" ");
                  for(j=0;j<=i;j++)
                        printf(" *");
                  row--;
                }
                printf("\n");
        }
}

}
return 0;
}

It compiles perfectly it just terminates after I enter the shape length. And that value is stored in row.

Sergei Steshenko 10-11-2010 04:41 PM

Quote:

Originally Posted by boilers969 (Post 4124263)
Its working just fine. The only thing I'm having problems with is terminating the program when the user enters a char other than s,h,f.

Code:

if(shape!=s/h/f)
        return 0;

This is not working. It's giving me the following error. Which says they are undeclared.
Code:

shape.c: In function âmainâ:
shape.c:22: error: âsâ undeclared (first use in this function)
shape.c:22: error: (Each undeclared identifier is reported only once
shape.c:22: error: for each function it appears in.)
shape.c:22: error: âhâ undeclared (first use in this function)
shape.c:22: error: âfâ undeclared (first use in this function)


And you dare to say you've read C99 standard and understood everything ?

Prove it ! Tell us the page number(s) and copy-paste the paragraphs justifying the piece of code in red.

boilers969 10-11-2010 06:06 PM

Good thing I figured it out so that doesn't even matter anymore. And I wasn't the one who came up with "s/h/f" just to let you know.

paulsm4 10-11-2010 06:19 PM

Uh, I'm not so sure it looks fine to me ;)

Code:

#include <stdio.h>

int
main (int argc, char *argv[])
{
  if (1 != 2 || 3 || 1)
    printf ("1 != 1\n");
  else
    printf ("1 != 2 or 3\n");
  return 0;
}

Quote:

gcc -Wall -pedantic -o tmp tmp.c
./tmp

1 != 1
<= Interesting result ;)
boilers969 -

Out of curiousity, what do the values (codes? abbreviations?) "s", "h" and "f" mean?

Or why is the variable "row" named "row"? I don't understand how you're using it as a row (or are you?)

boilers969 10-11-2010 06:23 PM

s, h, and f stand for the type of shape that the user wants printed. For example, s stands for straight line, h stands for half tip and f stands for full tip. And I named "row" "row" because it is the value of the number of row of the shape to be printed and it's easier to keep track of it that way.

Sergei Steshenko 10-11-2010 06:42 PM

Quote:

Originally Posted by boilers969 (Post 4124376)
Good thing I figured it out so that doesn't even matter anymore. And I wasn't the one who came up with "s/h/f" just to let you know.

Was it the C99 standard ? If yes, show where, if not, why didn't you verify the suggestion in C99 standard ?

You stubbornly refuse to get knowledge from the ultimate source of it.

boilers969 10-11-2010 07:02 PM

Quote:

Originally Posted by sag47 (Post 4123392)
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;
  }
}


He probably overlooked the mistake. And to honest I was probably thinking to fast and accidentally put a / instead of a comma. Its an honest mistake and I have always known it was wrong. But that part figured out now.

graemef 10-11-2010 07:43 PM

Back in post 49 I said that "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."

Your code has progressed from:
Code:

if(shape!=s/h/f)
to
Code:

if(shape!='s'||'h'||'f')
Which is an improvement but not quite there. In most programming languages you can't just say: "if shape doesn't equal 's' or 'f' or 'h'". But have to break it down to referring to the variable (shape in this case) with each term. So the English equivalent would be: "if shape doesn't equal 's' or shape doesn't equal 'f' or shape doesn't equal 'h'". More verbose, syntactically correct but logically wrong.

Looking at the logic problem if shape has the value 'x' then the three terms evaluate to: "true or true or true" which is true and is what is required. Now what happens if shape has the value 's'? The expression evaluates to "false or true or true" which evaluates to true.

To fix the logic problem you need to change the ors to ands giving in English: "if shape doesn't equal 's' and shape doesn't equal 'f' and shape doesn't equal 'h'"

paulsm4 10-11-2010 08:26 PM

Thank you, graemef. That's what I tried to illustrate. Let's put my code and your explanation together for boilers696, shall we?

Code:

  /* SAMPLE I GAVE ABOVE: WHY "||" SYNTAX IS WRONG! */
  if (1 != 2 || 3 || 1)
    printf ("1 != 1\n");

Quote:

// REASON:
I said that "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'...

In most programming languages you can't just say: "if shape doesn't equal 's' or 'f' or 'h'". But have to break it down to referring to the variable (shape in this case) with each term. So the English equivalent would be: "if shape doesn't equal 's' or shape doesn't equal 'f' or shape doesn't equal 'h'". More verbose, syntactically correct but logically wrong...

To fix the logic problem you need to change the ors to ands giving in English: "if shape doesn't equal 's' and shape doesn't equal 'f' and shape doesn't equal 'h'"
Code:

/* POSSIBLE CORRECTION */
  if ( (1 != 2) && (1 != 3) )
    printf ("1 != 1\n");

Code:

/* BETTER CORRECTION */
#include <stdio.h>

int
main(int argc, char *argv[])
{
  while(1)
  {
      char shape = -1;
      int row = -1;

      printf("Enter shape type (s/h/f): ");
      if (scanf("%c", &shape) != 1)
      {
        printf ("Excuse me: I didn't read \"shape\".  Please try again.\n");
        continue;
      }
      printf("Enter shape length: ");
      if (scanf("%d", &row) != 1)
      {
        printf ("Excuse me: I didn't read \"row\".  Please try again.\n");
        continue;
      }
      if(row<=0)
        printf("Shape length cannot be negative. Try again\n");

      switch(shape)
      {
        case 's':
        case 'S':
          printf ("Straight line....\n");
          break;
        case 'h':
        case 'H':
          printf ("Half tip....\n");
          break;
        case 's':
        case 'S':
          printf ("Full tip....\n");
          break;
        default:
          printf ("Done: finished program\n");
          return 0;
      }
  }
}


Sergei Steshenko 10-11-2010 11:08 PM

Quote:

Originally Posted by graemef (Post 4124428)
...
Which is an improvement but not quite there. ...

No it's not an improvement. It's a deterioration showing complete lack of understanding on the side of OP what types are for. I.e.

Code:

if(shape!='s'||'h'||'f')
performs boolean operation on 'char' data which here is complete nonsense.

boilers969 10-11-2010 11:35 PM

I finished it. Thanks for the help.

paulsm4 10-12-2010 12:56 AM

boilers969 -

Glad you got things working.

But for whatever it's worth, Sergei Steshenko is absolutely correct.

As you'll undoubtedly learn if you continue working with software ... it's very easy for a program to *appear* to work OK ... and yet have it fail completely once it leaves your hands.

It's far, far less important for a program to "run" than it is for you to UNDERSTAND the program. Inside and out.

Here's a short article that might help explain better:

http://www.pragprog.com/the-pragmati...ts/coincidence

Sincerely .. PSM


All times are GMT -5. The time now is 07:56 AM.