-5

The input is number of months and the two outputs are years and months. If I type 18 months should give me Years 1 Months 6

#include <stdio.h>


int main()
{
    int y, m;

    printf("Input number of months: ");
    scanf_s("%i", &y);
    y = (int) y / 12
    m = (int)y %;

    printf(" %i Year(s) \n %i Month(s)", y, m);
    return 0;
}
Ðаn
  • 10,400
  • 11
  • 57
  • 90
  • 1
    What exactly is the problem? Are you trying to compile this exact code, with `Int` instead of `int`, with `“”` instead of `""`, and so on? – HolyBlackCat Jan 16 '20 at 20:43
  • You're also missing a semicolon, and the line `m = (int) y %;` is nonsensical. How is this compiling – jeanluc Jan 16 '20 at 20:44
  • This doesn't compile does it? In `m = (int)y %;` the % is a binary operator meaning it requires two operands (one on each side of the %) Did you actually mean `m = (int)y % 12;` ? Plus, you don't need either of the casts - division of integers produces an integer result (and so does mod) – Jerry Jeremiah Jan 16 '20 at 20:44
  • Please post the **exact** code that you're having trouble with. Don't attempt to retype it, but cut-and-paste the actual code. – dbush Jan 16 '20 at 20:48
  • @JerryJeremiah It wouldn't compile according to C++ standard, only possible using some compiler extensions (allowing C headers to be used). Although... Yeah I don't know any compiler which wouldn't accept C headers in C++ code – Yksisarvinen Jan 16 '20 at 20:54
  • @Yksisarvinen I have never seen anyone ever mention that before. Even the C++ standard website says you can do it: https://isocpp.org/wiki/faq/mixing-c-and-cpp#include-c-hdrs-system – Jerry Jeremiah Jan 16 '20 at 21:12
  • @JerryJeremiah I suppose I took "deprecated" for "disallowed", but you are right, it should compile with any C++ compiler. Though it's unusual for `isocpp.org` to be mistaken, perhaps it was actually un-deprecated in newer standard? [stdio.h not standard in C++?](https://stackoverflow.com/questions/7596406/stdio-h-not-standard-in-c) – Yksisarvinen Jan 16 '20 at 21:28
  • @Yksisarvinen Fair enough - I concur. Deprecated is not standard. – Jerry Jeremiah Jan 16 '20 at 21:47
  • So when I run the program and I Input the number of months it gives me the same value for y and m. So like if I type 18 months it gives me Year 1 Month 1. Instead of Year 1 Months 6. I guess the issue is with m but again I’m not sure exactly what :( – Genesis Sandoval Jan 16 '20 at 23:31
  • @jeanluc what do you mean? Where am I missing the colon T_T – Genesis Sandoval Jan 16 '20 at 23:47

1 Answers1

0

There are many ways to do this, this is the one closest to your own code:

#include <stdio.h>

int main() {

    int y, m;

    printf("Input number of months: ");
    fflush(stdout);
    scanf("%d", &y);
    m = y % 12;
    y = y / 12;

    printf(" %i Year(s) \n %i Month(s)" , y, m);
    return 0;
}

The key issue is that you updated the value of y before m had a chance to look at it. Note the order of your y=... and m=.... If you do y=y/12 first, that would update y's value to the number of years and the remainder information is lost at this step and m would always be 0. By getting m from the y that's from the user's input first, you avoid that issue.

Another minor issue is that there is no need to cast the value from y/12 to be int since you are assigning that value to y which is of type int in the first place. Same applies to when you calculate m, where a typo is also present.

As for the use of fflush, you can refer to this question for details. Compile your code with/without this line to see the difference.

B.Gao
  • 146
  • 1
  • 8
  • More logical too to store input total month in `m` instead of `y`. – Jarod42 Jan 16 '20 at 20:51
  • @Jarod42 true, but I thought to keep my answer as close to the question as possible so that the key issue stands out. – B.Gao Jan 16 '20 at 21:00
  • I didn’t see your answer until now @B.Gao ! I think I understand now I guess I was too focus on the output for m. On my way home I’ll try it as soon as I get there and I’ll let you know!! :D – Genesis Sandoval Jan 16 '20 at 23:52