Home > Back-end >  asking for input again with do while loop in c
asking for input again with do while loop in c

Time:02-13

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:

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:

I hope this helps you. If it does please give green arrow.

   #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;
    }

CodePudding user response:

The key is Logical OR operator (||) in logical operator.

A logical operator would :

  1. treats its operands as boolean values
    • treats a non-zero value operand as true
    • treats a zero value operand as false
  2. operates the boolean algebra on its operands
  3. returns the boolean algebra result
    • returns 1 for true boolean result
    • returns 0 for false boolean result

The Logical OR operator (||) :

lhs || rhs
  • Here, lhs and rhs are the 2 operands of OR(||) operator
  • The OR operator returns 1 if any of the following conditions is true:
    1. only lhs is true
    2. only rhs is true
    3. both lhs and rhs are true
  • In other words, the boolean algebra OR result is true if either lhs or rhs is true.

In your example, there are 2 conditions :

  1. height is less than 1
  2. 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");
  • Related