Home > Blockchain >  Why is this C while didn't working anymore?
Why is this C while didn't working anymore?

Time:09-26

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

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related