first time asking, learning to program.
I am trying to have the user type in a number between 1 and 8 to print out stuff on screen (height), if the number is lower than 1 or higher than 8 I would like for the program to prompt again for an input instead of it quitting out and say "invalid". I know I have to set it up with do on top and when on the bottom in C, my question is how would I go about it when I have 2 possible outcomes, i.e. too low or too high?
do {
int height = get_int(Height: ");
if (height < 1)
~ ~printf("invalid");
else if (height > 8)
~ ~printf("invalid");
} when(???????)
printf("valid")
CodePudding user response:
The key is Logical OR operator (||
) in logical operator.
A logical operator would :
- treats its operands as boolean values
- treats a non-zero value operand as
true
- treats a zero value operand as
false
- treats a non-zero value operand as
- operates the boolean algebra on its operands
- returns the boolean algebra result
- returns
1
for true boolean result - returns
0
for false boolean result
- returns
The Logical OR operator (||
) :
lhs || rhs
- Here,
lhs
andrhs
are the 2 operands of OR(||
) operator - The OR operator returns
1
if any of the following conditions is true:- only
lhs
is true - only
rhs
is true - both
lhs
andrhs
are true
- only
- In other words, the boolean algebra OR result is true if either
lhs
orrhs
is true.
In your example, there are 2 conditions :
- height is less than 1
- height is larger than 8
if either of them is true, then the result is true (the loop will continue), so the expression in this example would be :
int height; /* move height outside the loop so it can be used in do-while condition */
do {
height = get_int();
if (height < 1)
printf("invalid\n");
else if (height > 8)
printf("invalid\n");
} while(height < 1 || height > 8);
printf("valid\n");
Furthermore, since we have already done the logical condition in the loop, we can just add a flag variable instead:
int flag; /* record the boolean result */
do {
int height = get_int();
if (height < 1) {
printf("invalid\n");
flag = 1;
} else if (height > 8) {
printf("invalid\n");
flag = 1;
} else {
flag = 0; /* !(height < 1 || height > 8) */
}
} while(flag);
printf("valid\n");
CodePudding user response:
Try this to see if it works for you.
If not, leave a comment to tell me what you really want.
#include <stdio.h>
int main()
{
int height = 0;
while (height < 1 || height > 8) {
scanf("%d", &height);
if (height >= 1 && height <= 8) {
break;
} else {
printf("Invalid, please try again.\n");
}
}
printf("Valid.\n");
return 0;
}
CodePudding user response:
A simple way of checking if the user has made an invalid choice would be to use a simple Boolean variable and set it to "false" when an invalid choice is made.
I have modified your own example code to include what I mean here:
bool valid = true;
do {
valid = true; //reset valid to true at the beginning of the loop, otherwise it will remain false when the loop runs again
int height = get_int("Height: ");
if (height < 1)
valid = false; //set valid to false when an invalid choice is made
else if (height > 8)
valid = false; //set valid to false when an invalid choice is made
} while (!valid) //if valid is not true ( or ==false), repeat. If valid is true or not equal to false, don't repeat and continue to the print statement below.
print("valid");
Please note that I used the same get_int
function you provided in your post. I am not aware of this function. For ways of reading integers (for example scanf
) see other answers in this thread.
CodePudding user response:
I hope this helps you.
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
do {
printf("Enter 0 to quit\n");
printf("Print Enter number between 1 and 8\n");
scanf("%d",&num);
if (num > 8) {
printf("Number to high\n");
} else if (num < 0) {
printf("Number to low\n");
}
if (num == 0) {
printf("You quit the program goodby\n");
} else {
printf("Number within range\n");
}
} while(num != -0);
return 0;
}