Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Checking string problem (http://www.go4expert.com/forums/checking-string-problem-t20947/)

cpulocksmith 10Feb2010 21:37

Checking string problem
 
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();
}


Gene Poole 10Feb2010 23:52

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

Code:

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


All times are GMT +5.5. The time now is 02:07.