Home > Net >  Using a do while loop correctly?
Using a do while loop correctly?

Time:02-16

My conditions are as follows :

  • If the value of userIncome is entered more than 400000, show error.
  • Similarly, if the value is less than 500, show error.

Repeat the loop until a correct value is entered.

#define _CRT_SECURE_NO_WARNINGS

#define minINCOME 500.00
#define maxINCOME 400000.00

#include <stdio.h>

int main(void)
{
    float userIncome ;


    printf(" -------------------------- \n"
           "    Wish List Forecaster   |\n"
           " -------------------------- \n");

   

    do
    {
        printf("Enter your monthly NET income: $");
        scanf(" %.2lf", &userIncome);

        if (userIncome < 500)
        {
            printf("ERROR: You must have a consistent monthly income of at least $500.00\n");
        }

        if (userIncome > 40000)
        {
            printf("ERROR: Liar! I'll believe you if you enter a value no more than $400000.00\n");
        }
    } while (userIncome >= maxINCOME && userIncome <= minINCOME);




    return 0;
}

CodePudding user response:

For starters this named constant

#define maxINCOME 400000.00

differs from the constant used in this if statement

 if (userIncome > 40000)

and in this condition

while (userIncome >= maxINCOME && userIncome <= minINCOME);

Secondly for object of the type float like this

float userIncome ;

you need to use the conversion specifier f instead of lf

scanf(" %f", &userIncome);

And the condition should be written like

while (userIncome >= maxINCOME || userIncome <= minINCOME);

And in if statements you need to use the defined named constants as for example

if (userIncome >= maxINCOME)
               ^^
  • Related