943,527 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1354
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Always defaulting to the else

Expand Post »
I'm creating a program that is supposed to have enumerated constants that contain days of the week. I am also supposed to have a variable called today that is of type week. Okay, I think I have done all of that correctly. Now I am supposed to assign a value to today. I think I've done that right as well. If it Monday-Friday, then one message displays. If it's the weekend, then you should get the other one. Now, everything looks right to me, but I keep getting the work day message. Is the user input failing to assign? That is the only thing that I can think that would be causing this.

  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5. {
  6. enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } today;
  7.  
  8. printf("What day is today? ");
  9. scanf("%c", &today);
  10.  
  11. if (today == Sunday || today == Saturday)
  12. printf("\nHooray, it's the weekend!\n");
  13. else
  14. printf("\nDrat, it's a work day.\n");
  15. return 0;
  16. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

An enum under the hood is stored by most compilers as an integer so the name Sunday is really a placeholder for the integer value 0. Monay evaluates to 1, Tuesday to 2 and so on.
You are giving the variable today the value of "Sunday" as an example. Because "Sunday" == Sunday is false you always execute the else.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

Okay, but I get the else even when I type in Monday or Tuesday. When I do that isn't it giving today the value of those days, which should result in: Hooray, it's the weekend! Only it doesn't.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

Try
today = Sunday; or today = Friday;
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

Click to Expand / Collapse  Quote originally posted by ddanbe ...
Try
today = Sunday; or today = Friday;
Like this?

  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5. {
  6. enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } today;
  7. today = Sunday;
  8. printf("What day is today? ");
  9. scanf("%c", &today);
  10.  
  11. if (today == Sunday || today == Saturday)
  12. printf("\nHooray, it's the weekend!\n");
  13. else
  14. printf("\nDrat, it's a work day.\n");
  15. return 0;
  16. }

That still always displays Drat, it's a work day.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

Comment out the scanf for the moment.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

I did that and it printed Hooray, it's the weekend! The thing is, however, that I need it to assign the day based on what the person enters. I thought that the scanf would do that.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Always defaulting to the else

The thing is that enum in C are a sort of integer constants.
So you could also use #define Sunday 0 to accomplish the same thing. You could let the user input a number or character and then add a case statement and say : (example) case 'S' : today = Sunday etc.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 7th, 2009
0

Re: Always defaulting to the else

Come come people. Look at the code. You can't read an integer using %c
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Jan 7th, 2009
0

Re: Always defaulting to the else

Welllll, you COULD enter CTRL-A to get Monday, CTRL-B for Tuesday... (but I wouldn't recommend it).

I like some form of menu in this case, something like:

What day of the week is it? (1-Sunday, 2-Monday, 3-Tuesday...)

Then you could switch on the character entered and use it to set 'today' so the compare would work.

If you don't mind making it a little confusing for the user (bad form in my opinion, but to each his own) you could prompt for the actual enum value and scanf into an int.

What day of the week is it? (0-Sunday, 1-Monday, 2-Tuesday...)

@RenFromPenn

What do you want the user to type in at your prompt?

Could you 'type up' what you think the interaction should look like, then we could help you get the code to do what you want.

For the first case of mine above it would look like:
  1. Please select the day of the week:
  2. 1 - Sunday
  3. 2 - Monday
  4. 3 - Tuesday
  5. 4 - Wednesday
  6. 5 - Thurdsay
  7. 6 - Friday
  8. 7 - Saturday
  9. What day of the week is it? 7
  10.  
  11. Hooray, it's the weekend!
  12.  
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C code program to Set/Change Linux time ???
Next Thread in C Forum Timeline: makes pointer from integer without a cast





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC