I am trying to set an error message for when the user gets wrong.

#include <cs50.h>
#include <stdio.h>

int main(void)
    int minutes, ounces, bottles;

        printf("How many minutes do you spend in the shower?\n");
        minutes = get_int();

        if(minutes < 1)
            printf("Error: Enter positive numbers only!\n");
    while(minutes < 1);

    ounces = (minutes * 192);
    bottles = (ounces / 16);    

    printf("Minutes: %i\n", minutes);
    printf("Bottles: %i\n", bottles);

All the tests are passing. I am doing this inside the do block as you may see above:

if(minutes < 0)
   printf("Error: Enter positive numbers only!\n");

I want to know if this is the proper way to do it or if there is another way to know the do block is repeating it?


What I might do is put the first printf before the loop, and then the second printf as an additional prompt that gets repeated as needed.

Something like

print "How many minutes? "
   print "Please enter a positive number: "
   get the int
while (it's negative)

So the first time it runs, the user sees:

"How many minutes? Please enter a positive number: "

and then, only if it needs to repeat, they'll just see:

"Please enter a positive number: "

I find that simpler than using an if statement inside, but that does work just as well. In your method, you are actually telling the user that they have an error. Either way. :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .