1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Checking string problem

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

  1. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    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

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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