How do I get the printf at the end of the while loop at runtime? he just misses it
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
return 0;
printf("Bottles: %i\n", min * 12);
}
OK, I'm really new to c. The code should work like this: if the user enters a negative integer, he will keep asking for an integer, otherwise he should run the printf statement at the end, but it doesn't. Can anyone help me?
Move the line return 0;
after the function printf
because it return 0;
lets your application finish. → So no printf("Bottles: %i\n", min * 12);
would be called.
int main(void)
{
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
printf("Bottles: %i\n", min * 12);
// Change the position of return
return 0;
}
As you are new to C,
return 0;
this means that it will return the value 0 after executing the program before it and exit the main () function. Nothing will be done after returning 0;
So, for your case, the line is
printf("Bottles: %i\n", min * 12);
not executed because that line is written after 0 is returned; statement.
So, put this line before returning 0; and your code should work fine!
change position
return 0;
from server use return, one is used to use break function
here your statement is return 0; breaking your main method and missing the following statements.
Now make it a habit to use return 0; at the end of the code
like this
int main(void)
{
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
printf("Bottles: %i\n", min * 12);
return 0; // always use before the right parenthesis of main function
}
Happy coding