enum

This is a discussion on enum within the C++ Programming forums, part of the General Programming Boards category; i get a error that says "use of enum "Days" without previous declaration" but i think i declared it, i ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    enum

    i get a error that says "use of enum "Days" without previous declaration" but i think i declared it, i think..
    here my code , im new still btw

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
        enum Days (sunday=1,monday,tuesday,wednesday,thursday,friday,saturday);
        Days off;
        cout <<"what day do you want off?(1-7)"<<endl;
        cin>>off;
        if(off == monday)
               {
               cout<<"Good choice monday is";
               }
        else
               {
                     cout <<off<<" is a good choice to";    
               }
                     getch();
    }
    kthanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    user@0[~]$ g++ -W -Wall -ansi -pedantic -O2 -o t t.c
    t.c:2:19: conio.h: No such file or directory
    t.c: In function `int main()':
    t.c:6: error: use of enum `Days' without previous declaration
    t.c:6: error: `sunday' undeclared (first use this function)
    t.c:6: error: (Each undeclared identifier is reported only once for each
       function it appears in.)
    t.c:6: error: `monday' undeclared (first use this function)
    t.c:6: error: `tuesday' undeclared (first use this function)
    t.c:6: error: `wednesday' undeclared (first use this function)
    t.c:6: error: `thursday' undeclared (first use this function)
    t.c:6: error: `friday' undeclared (first use this function)
    t.c:6: error: `saturday' undeclared (first use this function)
    t.c:7: error: `Days' undeclared (first use this function)
    t.c:7: error: parse error before `;' token
    t.c:9: error: `off' undeclared (first use this function)
    t.c:18: error: `getch' undeclared (first use this function)
    user@0[~]$
    One of your problems is this:
    Code:
    enum Days (sunday=1,monday,tuesday,wednesday,thursday,friday  ,saturday);
    You used () instead of {}.

    When you cin>>an enum, the user has to enter an integer, not "saturday" as you might expect.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yes that worked, i didnt notice i typed that in..
    but now i just notice that i get error on the line that says
    Code:
    cin>>off;
    any idea?
    btw my intent is to enter a number that corresponds with the day.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    I'm assuming you're getting an error saying that it's an invalid type. To input a value you'll have to get the input as an int and then set your enum from there:

    Code:
    int input;
    cin >> input;
    Last edited by jverkoey; 05-17-2006 at 05:25 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by jverkoey
    I'm assuming you're getting an error saying that it's an invalid type. To input a value you'll have to typecast that value to an int.

    Code:
    cin >> (int)off;
    didnt work, but u gave me the idea of what i had to do,,here the correct code incase anyone searchs enum

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
        enum Days {sunday=1,monday,tuesday,wednesday,thursday,friday,saturday};
        Days off;
        int x;
        cout <<"what day do you want off?(1-7)"<<endl;
        cin>>x;
        off = Days(x);
        if(off == monday)
               {
               cout<<"Good choice monday is";
               }
        else
               {
                     cout <<off<<" is a good choice to";    
               }
                     getch();
    }

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    *nods* That's exactly what you need to do. Be aware that it's possible for the user to type non-enumerated values because you're typecasting the int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 12:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21