Reputation: 29
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")
Upvotes: 0
Views: 2100
Reputation: 763
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.
Upvotes: 1
Reputation: 184
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;
}
Upvotes: 0
Reputation: 326
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;
}
Upvotes: 1
Reputation: 79
The key is Logical OR operator (||
) in logical operator.
A logical operator would :
true
false
1
for true boolean result0
for false boolean resultThe Logical OR operator (||
) :
lhs || rhs
lhs
and rhs
are the 2 operands of OR(||
) operator1
if any of the following conditions is true:
lhs
is truerhs
is truelhs
and rhs
are truelhs
or rhs
is true.In your example, there are 2 conditions :
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");
Upvotes: 2