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

Why is this C while didn't working anymore?

#include<stdio.h>

int main(void)
{
    int num;
    int week;
    int days;
    printf("enter a day\n");
    scanf_s("%d\n", &num);
    
    
    while (num <= 0)
    {
        printf("your input is wrong, try again");
        num++;
       while (num > 0)
    
        week = num / 7;
        days = week * 7 - num;
        printf("%d days are %d week and %d days\n", &num, &week, &days);

    }
    
    return 0;
}

I try to make a loop if the num<=0 then the program will back to at begin, but it doesn’t allow me to press any bottoms.

>Solution :

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

With all respect, all your code is wrong!

First of all, don’t put "%d\n" just use "%d" instead. On the same line, try to use "scanf".

Then by reading your code, what you were trying to do (I assume) is to enter a loop until the input was greater than 0, the problem is that you don’t do it that way. To do so, you need to use do/while instead.

Then your second while doesn’t have any curly braces. So, I did your code work just for fun 🙂

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num = 0, week = 0, days = 0;

    do
    {
        printf("Enter a day: ");
        if(!scanf("%d", &num))
            exit(EXIT_FAILURE); // It at the time of scanning a number we fail to do so, we will exit the program execution
        if(num <= 0)
            puts("Your input is wrong! Try again.");
    } while (num <= 0);

    week = num / 7;
    days = num - (week * 7);

    printf("%d days are: %d week and %d days.\n", num, week, days);

    return 0;
}

Hope it helps.

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