0

Hello i just started to learn C and im trying to make a code that takes a value of a day month and a year in numbers and it shows the month in words and limits the days but i cant succsed to do it if you can please tell me what im doing wrong

#include <stdio.h>

int main()
{
    int a,b,c;
        //day month year
    scanf("%d%d%d",&a,&b,&c);
    
    switch(b,a<=31)
    {
        case 1: printf("%d January %d",a,c+2000);
        break;
        case 2: printf("%d February %d",a,c+2000);
        break;
        case 3: printf("%d March %d",a,c+2000);
        break;
        case 4: printf("%d April %d",a,c+2000);
        break;
        case 5: printf("%d May %d",a,c+2000);
        break;
        case 6: printf("%d Juny %d",a,c+2000);
        break;
        case 7: printf("%d July %d",a,c+2000);
        break;
        case 8: printf("%d August %d",a,c+2000);
        break;
        case 9: printf("%d September %d",a,c+2000);
        break;
        case 10: printf("%d October %d",a,c+2000);
        break;
        case 11: printf("%d November %d",a,c+2000);
        break;
        case 12: printf("%d December %d",a,c+2000);
        break;
        
        
        default : printf("One of your numbers are wrong");
    }

}
  • 1
    `switch(b,a<=31)` what happens here? Don't you want just to switch on month? – KamilCuk Aug 23 '20 at 16:22
  • 1
    You need to limit the days using an `if` statement before the `switch` statement. Your current code is the same as `switch(a<=31)` which is either `switch(0)` or `switch(1)` – user3386109 Aug 23 '20 at 16:22
  • What you were not able to do ? Does the core fails for some input or it doesn't compile ? If so can you also provide that input ? – Deepak Patankar Aug 23 '20 at 16:22
  • 1
    Have a look at https://stackoverflow.com/q/52550/5265292 it's not really a duplicate, but basically it should contain the necessary information to understand what you code does. – grek40 Aug 23 '20 at 16:24
  • @user3386109 thank you i did a if statement and inside of it the switch it is now working :))) – שלומי גולן Aug 23 '20 at 16:27
  • Instead of a `switch` why not use an array for the month strings? Then you could do something like `printf(%d %s %d", a, month_names[b - 1], c + 2000)` – Some programmer dude Aug 23 '20 at 16:29
  • I also recommend that you give your variables names that are a little more meaningful. It will make your code much more easier to read and understand. – Some programmer dude Aug 23 '20 at 16:30
  • @Someprogrammerdude yeah you are probably right, its just something i needed to do for class... i tried something else but the proffesor wanted it that way so this is why i came here. thank you very much anyways :) – שלומי גולן Aug 23 '20 at 16:33

3 Answers3

3

switch condition can take only one variable. Since, a & c are constants here, they should be used as is.

#include <stdio.h>

int main()
{
    int a,b,c;
        //day month year
    scanf("%d%d%d",&a,&b,&c);
    // date validation should be done before and if it is valid. then only process further
    if (a < 1 || a > 31) {
        cout << "Invalid date.";
        exit(1);
    } 
    
    switch(b)
    {
        case 1: printf("%d January %d",a,c+2000);
        break;
        case 2: printf("%d February %d",a,c+2000);
        break;
        case 3: printf("%d March %d",a,c+2000);
        break;
        case 4: printf("%d April %d",a,c+2000);
        break;
        case 5: printf("%d May %d",a,c+2000);
        break;
        case 6: printf("%d Juny %d",a,c+2000);
        break;
        case 7: printf("%d July %d",a,c+2000);
        break;
        case 8: printf("%d August %d",a,c+2000);
        break;
        case 9: printf("%d September %d",a,c+2000);
        break;
        case 10: printf("%d October %d",a,c+2000);
        break;
        case 11: printf("%d November %d",a,c+2000);
        break;
        case 12: printf("%d December %d",a,c+2000);
        break;
        
        
        default : printf("One of your numbers are wrong");
    }

}

Note: Date is not properly validated. Better to create a function and validate it first. Ex. check like February can't have more than 29 days.

Amit Kumar
  • 1,780
  • 1
  • 10
  • 8
3

i added an if statement before of it and it worked thank you guys very much :)

int a,b,c;
            //day month year
        scanf("%d%d%d",&a,&b,&c);
        if(a<=31)
        {
        switch(b)
        {
            case 1: printf("%d January %d",a,c+2000);
            break;
            case 2: printf("%d February %d",a,c+2000);
            break;
            case 3: printf("%d March %d",a,c+2000);
            break;
            case 4: printf("%d April %d",a,c+2000);
            break;
            case 5: printf("%d May %d",a,c+2000);
            break;
            case 6: printf("%d Juny %d",a,c+2000);
            break;
            case 7: printf("%d July %d",a,c+2000);
            break;
            case 8: printf("%d August %d",a,c+2000);
            break;
            case 9: printf("%d September %d",a,c+2000);
            break;
            case 10: printf("%d October %d",a,c+2000);
            break;
            case 11: printf("%d November %d",a,c+2000);
            break;
            case 12: printf("%d December %d",a,c+2000);
            break;
            
            
            default : printf("One of your numbers are wrong");
        }
       }
       else printf("You entered a wrong day");
1
(b,a<=31)

no!

(b)

works, but you are trying to do something else

((a<=31)?b:0)

is what you want. It's the ternary operator. We can do an if inside an expression that way.

Joshua
  • 40,822
  • 8
  • 72
  • 132