4
#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 am really new at c. The code should work like this: If the user types in a negative integer it will continue to ask for the integer otherwise it should run the printf statement at the end but that is not happening. Can someone help me please?

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Zayn
  • 61
  • 4

3 Answers3

4

Move the return 0; line after the printf function, because the return 0; let your application finish. --> So no printf("Bottles: %i\n", min * 12); would have been 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;

}
wake-0
  • 3,918
  • 5
  • 28
  • 45
4

As you are new in C,

return 0;

this means it will return 0 value after executing the program up to it and will exit the main() function. Nothing will be executed after return 0;

So for your case, the line printf("Bottles: %i\n", min * 12); is not executed because this line is written after the return 0; statement.

So put that line before return 0; and your code should run fine !

Ebrahim Khalilullah
  • 556
  • 1
  • 6
  • 10
3

change the position of

return 0;

from serveral use of return,one is return use to break function

here your return 0; statement breaking your main method and skiping following statements.

From now, make a habit to use return 0; in the end of 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

SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44