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

main() {
    char d[10];
    int value = 0, val1, val2;
    printf("Enter Day: ");
    scanf("%c", &d);

    val1 = strcmp(d, "sunday");
    val2 = strcmp(d, "saturday");
    
    if (val1 == 0) {
        printf("AS");
        value = 2;  
    } else
    if (val2 == 0) {
        value = 1;   
    } 
       
    switch (value) {
      case 2:
        printf("So sad, you will have to work");
        break;
      case 1:
        printf("Enjoy! its holiday");
        break;
      default:
        printf("Print valid character");
    }
}

I enter code here want to input days and to get some output using switch statement but strcmp is not working in if statement I have to use a switch statement also if statement not recognising value.

4
  • 1
    The notation main() { … } has been non-standard for the whole of this millennium; it was made non-standard in C99. You should not be writing obsolete code! You should explicitly specify the return type (int), and it is best to use int main(void) when your program does not heed command-line arguments. Commented Dec 17, 2020 at 7:36
  • 1
    The code shown does not use any feature from <conio.h> AFAICS; omit the header. You should include a break; after the default: case, as a protection against future enhancements. You should output a newline at the end of the various printf() statements — get into the habit of terminating outputs with newlines. Commented Dec 17, 2020 at 7:38
  • 1
    The bug: scanf("%c",&d);. Should be %s. Voting to close as simple typo. Commented Dec 17, 2020 at 7:42
  • You made the strcmp too complicated. You do not need an addtional switch. This makes the code more difficult to read. Commented Dec 17, 2020 at 7:43

1 Answer 1

2

At least this problem:

strcmp(d,"sunday") expects array d to contain a string.

d does not certainly contain a string as no null character was assigned there.

char d[10];
printf("Enter Day: ");
scanf("%c",&d);  // Writes, at most, 1 character to `d`. Remainder of `d[]` is uninitialized.

Instead

if (scanf("%9s",d) == 1) {
  printf("Success, read <%s>\n", d);

Tip: Consider using fgets() to read user input.

Tip: Enable all compiler warnings to save time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.