Always defaulting to the else

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Always defaulting to the else

 
0
  #1
Jan 4th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,962
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 284
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Always defaulting to the else

 
0
  #2
Jan 4th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Always defaulting to the else

 
0
  #3
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,962
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 284
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Always defaulting to the else

 
0
  #4
Jan 4th, 2009
Try
today = Sunday; or today = Friday;
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Always defaulting to the else

 
0
  #5
Jan 4th, 2009
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,962
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 284
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Always defaulting to the else

 
0
  #6
Jan 4th, 2009
Comment out the scanf for the moment.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Always defaulting to the else

 
0
  #7
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,962
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 284
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Always defaulting to the else

 
0
  #8
Jan 4th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Always defaulting to the else

 
0
  #9
Jan 7th, 2009
Come come people. Look at the code. You can't read an integer using %c
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 561
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 90
Murtan Murtan is offline Offline
Posting Pro

Re: Always defaulting to the else

 
0
  #10
Jan 7th, 2009
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.  
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC