Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the printf at the end of do while loop to be executed? it just skips it

Tags:

c

cs50

#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?

like image 632
Zayn Avatar asked Dec 18 '22 07:12

Zayn


1 Answers

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;

}
like image 196
Kevin Wallis Avatar answered May 20 '23 02:05

Kevin Wallis