LinuxQuestions.org
Review your favorite Linux distribution.
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-11-2010, 04:40 PM   #91
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0

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.
 
Old 10-11-2010, 04:41 PM   #92
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by boilers969 View Post
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.
 
0 members found this post helpful.
Old 10-11-2010, 06:06 PM   #93
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
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.
 
Old 10-11-2010, 06:19 PM   #94
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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?)

Last edited by paulsm4; 10-11-2010 at 06:23 PM.
 
Old 10-11-2010, 06:23 PM   #95
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
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.
 
Old 10-11-2010, 06:42 PM   #96
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

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

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sag47 View Post
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.
 
Old 10-11-2010, 07:43 PM   #98
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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'"
 
2 members found this post helpful.
Old 10-11-2010, 08:26 PM   #99
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

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

Last edited by paulsm4; 10-11-2010 at 08:29 PM.
 
1 members found this post helpful.
Old 10-11-2010, 11:08 PM   #100
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
...
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.
 
1 members found this post helpful.
Old 10-11-2010, 11:35 PM   #101
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
I finished it. Thanks for the help.
 
Old 10-12-2010, 12:56 AM   #102
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:45 AM.

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