Thread: Days of the week program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Days of the week program

    Hello!

    I am stuck on this program I have to write for the days of the week. The days of the week is in a string array, and the program asks the user for a number between 1 and 7. The program is then supposed to spit out the corresponding day of the week. There also needs to be an error message if the user enters anything besides 1-7. This is what I got, and it keeps telling me it's Sunday no matter what I enter! Could anyone please lend me a hand to find out what is wrong here?

    Code:
    # include <stdio.h>
    
    int main()
    {
            int day;
            char *weekday[7] =
            {
               "Sunday",
               "Monday",
               "Tuesday",
               "Wednesday",
               "Thursday",
               "Friday",
               "Saturday"
            };
    
            printf ("Enter number between 1-7: ");
            scanf ("%d", &day);
            printf("\nThe day of the week is:\n\n");
    
            if ((day=1))
            {
                    printf("%s", weekday[0]);
            }
    
            else if ((day=2))
            {
                     printf("%s", weekday[1]);
            }
    
            else if ((day=3))
            {
                    printf ("%s", weekday[2]);
            }
    
            else if ((day=4))
            {
                     printf ("%s", weekday[3]);
            }
    
            else if ((day=5))
            {
                    printf ("%s", weekday[4]);
            }
    
            else if ((day=6))
            {
                    printf ("%s", weekday[5]);
            }
    
            else if ((day=7))
            {
                    printf ("%s", weekday[6]);
            }
    
            else
            {
                    printf("Error");
            }
    
            return 0;
    }
    Thank you!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You need to use == to compare values, not =. A single = is assignment. When you do if(day = 1) then you're assigning 1 to day, and the result is if(1), which is always true. I presume you put an extra pair of parentheses around the statements because your compiler was complaining. It was right! You don't want assignment here.

    But there is a much easier way to accomplish this: first, make sure day is between 1 and 7. Then you can simply do weekday[day - 1].

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thank you! And yes, the reason I put the double parenthesis there is because the compiler wasn't happy. But, for some reason, I thought that would fix the problem too (I thought it was either use == OR ((...)) for some reason). I have to go to class now, but I will fix this afterwards. I will post if I have any more complications. Thank you again!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Referring to message #1... everything from line 21 to line 55 can be reduced to...
    Code:
    printf("%s",weekday[day - 1]);
    Why day - 1? Because it is common for people to enter days from 1 to 7... but C arrays actually start at 0 giving you a range from 0 to 6... the -1 merely offsets the range to give you the right day.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    A matter of personal choice I suppose, but when dealing with constants or literals,
    Code:
    if( 1 = day )
    will never compile and you'll hopefully be pointed directly to the problem.
    Code:
    if( day = 1 )
    On the other hand, this second one will compile, and depending on your compiler or warning settings, may not complain at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Days of the Week!!!!
    By psyc0pth in forum C++ Programming
    Replies: 5
    Last Post: 01-17-2011, 11:30 AM
  2. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  3. Replies: 47
    Last Post: 01-04-2008, 05:24 AM
  4. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  5. sorting days of the week
    By brodacz in forum C Programming
    Replies: 3
    Last Post: 08-13-2002, 12:36 PM