0

I'd like to write a program to convert days into years, weeks, and days. I wrote the program, but there is a condition that if the user inputs a character, the output should be Only positive numeric is allowed. But, when I enter a character, it picks the ASCII value of that. I'm not able to understand what condition to put.

#include<stdio.h>

int main(){
    
    int days, year, weeks;
    printf("Enter days: ");
    scanf("%d", &days);
    
    if(days)
    {
        year = days/365;
        printf("\nYears: %d\n", year);

        weeks = (days-(year*365)) / 7;
        printf("Weeks: %d\n", weeks);
        
        days = days-(year*365)-(weeks*7);
        printf("Days: %d\n", days);
    }
    else{
        printf("Only positive numeric is allowed.");
    }

    return 0;
}

Desired Output:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    Step 1: Check return value of `scanf("%d", &days);`. – chux - Reinstate Monica Oct 14 '21 at 23:57
  • 1
    You should use the return value from `scanf` to decide if acceptable input has been read or not, and always initialize your variables to a known good value. If `scanf` fails what is the value of `days`? Might be 0, might not, it isn't guaranteed to be anything but indeterminate. – Retired Ninja Oct 14 '21 at 23:58
  • [How to validate input using scanf](https://stackoverflow.com/questions/456303/how-to-validate-input-using-scanf) may be helpful. You might want to pick one tag from C or C++, they are different languages with different rules. Based on your code only the C tag is appropriate. – Retired Ninja Oct 15 '21 at 00:02
  • Make sure you are not confusing restrictions on the input you will get with requirements for error handling. Many early homeworks do not require you to bother error checking for bad input, as all input will be good. – Dúthomhas Oct 15 '21 at 00:03
  • Since you tagged as C++, you could eliminate the *format specifiers* by using `std::cin` and `std::cout`. – Thomas Matthews Oct 15 '21 at 00:06
  • BTW, the `if (days)` is false for zero, but true for other positive *and negative* values. You should be more specific, like `if (days > 0)`. – Thomas Matthews Oct 15 '21 at 00:07
  • A big issue with your calculation is leap years. Some years have 366 days. Or you could divide by 365.25. – Thomas Matthews Oct 15 '21 at 00:09
  • Why do you have a calculation for days? – Thomas Matthews Oct 15 '21 at 00:11
  • A week is 7 days. The number of weeks would be: `weeks = days / 7;`. – Thomas Matthews Oct 15 '21 at 00:12
  • @chux-ReinstateMonica Thanks. – Rohan Singh Oct 15 '21 at 00:20
  • @ThomasMatthews 1. Teacher told us to ignore the leap day. 2. I did calculate for days because after converting the given days into years and weeks, I have to print how many days left. – Rohan Singh Oct 15 '21 at 00:24
  • @RohanSingh Code simplification: `weeks = (days-(year*365)) / 7;` --> `weeks = (days%365) / 7;`. – chux - Reinstate Monica Oct 15 '21 at 00:27
  • @RohanSingh A more common reduction of days to year,week,days: `year = days/365; days %= 365; week = days / 7; days %= 7;`. – chux - Reinstate Monica Oct 15 '21 at 00:35
  • Minor detail: if **you know** it is not negative, it should be unsigned. [and in that case: you should use "%u" as a format specifier. and check the return value] – wildplasser Oct 15 '21 at 00:40

1 Answers1

1
  • Code needs to use the return value of scanf() to determine scanning success.

  • A range test is needed for "positive numbers".


// scanf("%d", &days);
// if(days)
if (scanf("%d", &days) == 1 && days > 0)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256