Member Avatar for RenFromPenn

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.

#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;
 }

Recommended Answers

All 15 Replies

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.

Member Avatar for RenFromPenn

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.

Try
today = Sunday; or today = Friday;

Member Avatar for RenFromPenn

Try
today = Sunday; or today = Friday;

Like this?

#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.

Comment out the scanf for the moment.

Member Avatar for RenFromPenn

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.

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.

Come come people. Look at the code. You can't read an integer using %c

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:

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!
Member Avatar for RenFromPenn

I want to ask them with a prompt, "What day is it?

I then want them to enter Sunday, Monday, etc.

I used %c because I was trying to reading the name of the day, which would be a set of characters.

You will have to declare an array of characters to input into, you can't input directly into the enum.

The % format for an array of characters is %s

Then you will have to compare what they typed against a list of strings you have to 'look up' the proper enum.

What do you want to do if the user enters a string that doesn't match any of the days?

(What day is it? Orange)

I want to ask them with a prompt, "What day is it?

I then want them to enter Sunday, Monday, etc.

I used %c because I was trying to reading the name of the day, which would be a set of characters.

No, you don't want this. At least not in total.
First, %c reads a single character
Second, %s reads a string, but you don't want to use it. Here's why
Third, it also makes your code much more complicated. You have to read in the string, then convert that string to the correct integer. Instead, how about prompting: What day is it (0=Sunday, 1=Monsay...)?

Member Avatar for RenFromPenn

No, you don't want this. At least not in total.
First, %c reads a single character
Second, %s reads a string, but you don't want to use it. Here's why
Third, it also makes your code much more complicated. You have to read in the string, then convert that string to the correct integer. Instead, how about prompting: What day is it (0=Sunday, 1=Monsay...)?

That is how I had already decided to set it up since asking for the day by name wasn't working.

delete this

it looks like finished business to me but i will say it anyway....
using enumeration, for example enum colors {RED, BLUE, YELLOW, GREEN} favouriteColor; you actually make an integer variable that can be assigned integer values using keywords you define. to make it simple:

#include <stdio.h>

enum colors {RED, BLUE, YELLOW, GREEN, ORANGE, VIOLET} favouriteColor;
enum shape {TRIANGLE, SQUARE, RECTANGLE ,CIRCLE, OVAL}

main ()
{
   shape favouriteShape; // This is just a demonstration of second way to define a variable
   
   favouriteColor = BLUE;
   favouriteShape = SQUARE;
}

what enumeration actually does is making your code much more readable.
consider this:

favouriteColor = 1;
   favouriteShape = 1;

and if you have tons of statements like these you will soon find yourself in mass of numbers where you dont know what numbers actually mean, and you would have to go to enumeration on the begining of the code to say: "Ah, yes...2 was YELLOW."

you cannot write something like:

printf ("What is your favourite color?");
   scanf ("%d", favouriteColor);

and enter word RED on input, because facouriteColor is an integer.
you also cant use scanf ("%s", favouriteColor); instead, because favouriteColor is far from being a string of chracters

if you want to use form you mentioned on the begining, something like: "What day is it?", you should use array of chars for input, and than compare it with constant strings: "Sunday", "Monday" ... using function strcmp(string1, string2) , where strcmp evaluates (equals) 0 if they are the same, <0 if string1<string2 and >0 if string1>string2 is greater (by their ascii values). you can also use strcmpl(string1, string2) , it is same as first one but doesnt take small and capital letters into account. i dont have any other methods for this on my mind right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.