C program that displays shapes. Keep getting errors.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
#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.
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'"
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;
}
}
}
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.