1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Checking string problem

Discussion in 'C' started by cpulocksmith, Feb 10, 2010.

  1. cpulocksmith

    cpulocksmith New Member

    Having an issue with checking if a string is a certain word or another. The program just crashes when it gets to the check.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char dayName[10];
        int numDays; // number of days
        int startDay;
        int endDay;
    
        printf("Enter a day of the week:\n");
        scanf("%s", dayName);
        printf("Enter the number of days from that day:\n");
        scanf("%i", numDays);
        
        if (dayName == "Sunday") // Errors here. Prolly doin it wrong
        {
                    startDay = 1;
        }
        else if  (dayName == "Monday")
        {
                    startDay = 2;
        }
        else if  (dayName == "Tuesday")
        {
                    startDay = 3;
        }    
        else if  (dayName == "Wednesday")
        {
                    startDay = 4;
        }    
        else if  (dayName == "Thursday")
        {
                    startDay = 5;
        }    
        else if  (dayName == "Friday")
        {
                    startDay = 6;
        }    
        else if  (dayName == "Saturday")
        {
                    startDay = 7;
        }else{
              printf("Error");
              }
              
        
        endDay = (startDay + numDays) % 7;
        
        switch(endDay)
        {
                      case 1:
                           printf("It would be Sunday");
                           break;
                      case 2:
                           printf("It would be Monday");
                           break;
                      case 3:
                           printf("It would be Tuesday");
                           break;
                      case 4:
                           printf("It would be Wednesday");
                           break;
                      case 5:
                           printf("It would be Thursday");
                           break;
                      case 6:
                           printf("It would be Friday");
                           break;
                      case 7:
                           printf("It would be Saturday");
                           break;
                      default:
                              printf("Error");
        }
        
        getchar();
        getchar();
    }
    
     
  2. Gene Poole

    Gene Poole New Member

    dayName is an array of char. There is no == operator for arrays. You'll need to use strcmp:

    Code:
      if (strcmp(dayName, "Sunday")==0)
    
     

Share This Page