Thread: List of Enumerated constants

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    List of Enumerated constants

    OK Here is the problem that I am trying to do:
    Create a set/list of enumerated constants called week that contains the days of the week. Have a variable called today that is of type week. Assign a value to today. If the day is Monday through Friday, print "Go to work!" If the day is Saturday or Sunday, print "You can rest today!"
    I thought that I did it correctly, but I cant get it to work right. It says "Go to work" for everyday. Any suggestions? Here is my code:

    Code:
    #include <stdio.h> 
    
    enum week{ Monday=0,Tuesday=1,Wednesday=2,Thursday=3,Friday=4,Saturday=5,Sunday=5 }; 
    
    int main() 
    { 
        enum week today; 
        int day; 
        printf("Enter the day of the week(Sunday-Saturday): "); 
        scanf("%s", &day); 
        switch (day) 
        { 
          case 0: day = Monday; break; 
          case 1: day = Tuesday; break; 
          case 2: day = Wednesday; break; 
          case 3: day = Thursday; break; 
          case 4: day = Friday; break; 
          case 5: day = Saturday; break; 
          case 6: day = Sunday; break; 
        } 
        if (day >5) 
        { 
            printf("\nGo to work!"); 
        } 
        else 
        { 
            printf("\nYou can rest today!"); 
        } 
        return 0; 
    }


  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    scanf("%s", &day);
    So you are trying to read in a string into an int variable? It seems more like you want to read in a number? In which case you should use scanf("%d", &day); that might fix your problem..Of course you're expecting the user to intrinsically know the numbers for each day with no help which is kind of not very user friendly right?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, it's pretty close. I found some mistakes.
    Code:
        int day;
        printf("Enter the day of the week(Sunday-Saturday): ");
        scanf("%s", &day);
    The input is actually an integer, but you are asking scanf to grab string input. You should be using "%d" or "%i" as a format string there. The prompt can also be edited to reflect what the user should type, e.g.: "Enter day of the week (0 = Monday, 6 = Sunday):"

    Code:
        if (day >5)
        {
            printf("\nGo to work!");
        }
    This person only works on weekends, as well.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, it might be a good idea to check the range of the input entered. One more thing, most people count off the days of the week starting at one, not zero.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    Thanks, I will try to fix it like that. I was a little confused (Obviously!) with the enumerated list. And sorry my brain function obviously wasnt working to use > instead of <.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with enumerated data types.
    By And3rs in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2008, 10:06 AM
  2. enumerated types
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2003, 02:59 PM
  3. Enumerated Constants
    By Eminence in forum C++ Programming
    Replies: 3
    Last Post: 07-28-2003, 08:04 AM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

Tags for this Thread