Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a do while loop correctly?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
               ^^
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading