| | |
Always defaulting to the else
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } today; printf("What day is today? "); scanf("%c", &today); if (today == Sunday || today == Saturday) printf("\nHooray, it's the weekend!\n"); else printf("\nDrat, it's a work day.\n"); return 0; }
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Like this?
That still always displays Drat, it's a work day.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } today; today = Sunday; printf("What day is today? "); scanf("%c", &today); if (today == Sunday || today == Saturday) printf("\nHooray, it's the weekend!\n"); else printf("\nDrat, it's a work day.\n"); return 0; }
That still always displays Drat, it's a work day.
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: May 2008
Posts: 570
Reputation:
Solved Threads: 93
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:
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:
C Syntax (Toggle Plain Text)
Please select the day of the week: 1 - Sunday 2 - Monday 3 - Tuesday 4 - Wednesday 5 - Thurdsay 6 - Friday 7 - Saturday What day of the week is it? 7 Hooray, it's the weekend!
![]() |
Similar Threads
- execute python script from shell/Interactive Window (Python)
- Exit Codes (Python)
- New Hard Drive, now what? (Apple Hardware)
- Browser redirect/about:blank/other problems... (Viruses, Spyware and other Nasties)
- Problems when viewing pages... (Web Browsers)
- wininet.dll after SP2 upgrade (Windows NT / 2000 / XP)
- Home page gets directed to index page (Viruses, Spyware and other Nasties)
- Great search homepage (HIjack log inside) (Viruses, Spyware and other Nasties)
- w2k server errors (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: C code program to Set/Change Linux time ???
- Next Thread: makes pointer from integer without a cast
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






