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.
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?
#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)
}
}
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.
I believe you have to signify that a char has been selected. Strings are wrapped in double quotes " and char (or single characters) are wrapped in single quotes '. So you would need something along the lines of.
There the if statements are comparing the value stored in the shape variable to different conditions. Like if the contents of shape is equal the the character b then do something. Here is the exact same statement in a case...
Code:
char shape=1;
shape='c';
switch(shape)
{
case 'a':
return 0;
case 'b':
return 0;
default:
return 1;
}
It does the same thing as the if statements. Does this explain the how the case works a little?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.