-1

I have a programming class and currently has a homework to show the holiday name based on the month and day entered by a user in C program.

Whenever I compile and run the program, there is no error. However, if I enter any month and hit enter, it just returns New year's day and it does not ask for the date. It is supposed to require both the month and the date then display the corresponding holiday but it seems to be not working.

I was wondering if you could provide inputs on what would be the correct syntax.

Below is the code I tried to create:

#include <stdio.h>

int main () {

    char m;
    int d;

    printf("Enter Month: ");
    scanf("%c", &m);
    printf("Enter Date: ");
    scanf("%d",&d);

    if (m ="January" && d == 1)
        printf("New year's day ");
    else if (m ="July" && d ==1 )
        printf("Canada day ");
    else if (m = "December" && d ==25)
        printf("Christmas day ");
    else
        printf("%c %d does not correspond to a fixed-date holiday ", m, d);

    return 0;
}
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
JM Roberto
  • 13
  • 1
  • 3
  • 5
    I think you should go back to whatever text-book or tutorial you're reading, and start over. You have multiple problems with the code you show, telling me you're kind of guessing about things than actually knowing anything. – Some programmer dude Jun 06 '18 at 05:54
  • Read up on the differences of the followoing pairs: type `char` and someting like "January". `=` and `==`. Comparing strings and `==`. Also join the fun of all the problems realted to `scanf()`. http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq – Yunnosch Jun 06 '18 at 06:00
  • `char m` is far too short to hold a non-null string. – Sourav Ghosh Jun 06 '18 at 06:02
  • Let me try and make it little simpler for you, try and ask yourself some **questions.** 1) Why do the two comparison statements look different in if block. 2) Is it actually not asking for input or is it. Try following your guide line by line, and you will figure your error out – vipulbhj Jun 06 '18 at 06:02

1 Answers1

0

Things you have to change in your program

1 - Do not Use char type to store a string, you can use an array of characters.
2 - = is not a comparison operator, it is an assignment operator, so you should use a function strcmp() which is used to compare two string and return the number of different characters (and 0 if both strings are same).

#include <stdio.h>
#include<string.h>

int main()
{
    char m[10];
    int d;
    printf("Enter Month: ");
    scanf("%s", m);
    printf("Enter Date: ");
    scanf("%d", &d);

    if ( !(strcmp(m, "January")) && d == 1)
        printf("New year's day ");
    else if (!(strcmp(m, "July")) && d == 1)
        printf("Canada day ");
    else if (!(strcmp(m, "December")) && d == 25)
        printf("Christmas day ");
    else
        printf("%s %d does not correspond to a fixed-date holiday ", m, d);

    return 0;
}
Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
  • You're awesome! @Siraj Thanks for explaining and correcting the syntax. I'm still a newbie in coding and this is really helpful. We have not tackled strcmp() yet that's why I have no idea. I appreciate your input. :) – JM Roberto Jun 07 '18 at 14:01
  • I'm glad i could helped you. – Siraj Alam Jun 07 '18 at 18:48