Last visit was: It is currently Thu Mar 28, 2024 3:45 pm


All times are UTC-05:00




Post new topic Reply to topic  [7 posts ] 
    Author Message
     Post subject:Need help with small C++ program: Returns! :P
    PostPosted:Thu Aug 23, 2007 9:25 am 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    Okay, I have been reading the tutorials at learncpp.com (which imo are the best tutorials yet, 4.8/5 stars).

    I have been experimenting with enum types... well, just check out the code:

    species.cpp:
    Code:
    #ifndef SPECIES_CPP #define SPECIES_CPP #include "Includes.h" #include "species.h" #endif int PickSpecies() { Species eSpecies; cout << "Which species would you like to be?" << endl; cout << "1. Human\n2. Dog\n3. Cat\n4. Bob\n Enter the number of your choice: "; cin >> eSpecies; switch (eSpecies) { case speciesHuman: cout << "You are a Human.\n"; break; case speciesDog: cout << "You are a Dog.\n"; break; case speciesCat: cout << "You are a Cat.\n"; break; case speciesBob: cout << "You are a Bob.\n"; break; default: cout << "You didn't enter a number..."; break; } return eSpecies; }
    species.h:
    Code:
    #ifndef SPECIES_H #define SPECIES_H #include "Includes.h" #endif enum Species { speciesHuman = 1, speciesBob = 4, speciesDog = 2, speciesCat = 3 };
    Here's my error:
    species.cpp(14) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Species' (or there is no acceptable conversion)

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 10:03 am 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    Since Species is not directly a int there is no >> operator defined.

    Try?
    Code:
    int eSpecies;

    Edit: other comments:

    species.cpp: remove, .cpp files are compiled only once.
    Code:
    #ifndef SPECIES_CPP #define SPECIES_CPP ... #endif
    species.h: Should look like
    Code:
    #ifndef SPECIES_H #define SPECIES_H #include "Includes.h" enum Species { speciesHuman = 1, speciesBob = 4, speciesDog = 2, speciesCat = 3 }; #endif
    Also Includes.h is a strange name for a include file ;) (just style here)


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 10:09 am 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    Fixed.

    Thanks.

    But why is the >> any part of this? It's a part of io.

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 11:40 am 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    Or I guess you could overload the >> operator for Species:
    Code:
    // Overload the >> operator for Species // istream is an input stream (could be a file, a network socket, console input (ie: cin), ...) // the reason this function return istream& is to able to chain up >> (ex: cin >> inta >> intb >> intc; ) // &is is the input stream on the left side of the operator (ex: cin >> inta; // is& would be cin) istream& operator >> (istream &is, Species &spe) { int intSpecie; is >> intSpecie; spe = static_cast<Species>(intSpecie); return is; }
    And then keep your original code in PickSpecies.


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 11:52 am 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    This is my program so far (I was bored so I decided to do this, I've learned a couple things from it, I don't really think that there is a point to this program...)

    http://www.e-ringtech.com/Tut1.exe


    P.S. You guys should really either upgrade to phpBB3 or install an attachment mod...

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 6:46 pm 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    .exe ? Plain source or nothing, really.


    Top
    Offline  
     Post subject:
    PostPosted:Thu Aug 23, 2007 8:18 pm 
     

    Joined:Thu Aug 09, 2007 9:09 am
    Posts:26
    I see two problems with this program as written. The first is that your value is not an integer; it is a Species. So, declare the function as returning a Species value, not an int. You likewise should not be treating the Species values as ints without casting them. An enum data type is an abstraction; the point of it is that it shouldn't ever hold values outside its range. Don't worry about how the compiler represents it internally.

    The second is that the function doesn't check its input at all, so it can easily return garbage. At minimum, you'd need to check cin::fail() to see whether the user really typed in an integer, and then check the range to see whether the integer mapped to a valid species.


    Top
    Offline  
    Display posts from previous: Sort by 
    Post new topic Reply to topic

      All times are UTC-05:00


      Who is online

      Users browsing this forum: No registered users and 20 guests


      You cannot post new topics in this forum
      You cannot reply to topics in this forum
      You cannot edit your posts in this forum
      You cannot delete your posts in this forum
      You cannot post attachments in this forum

      Search for:
      Jump to:  
      cron
      Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
      Theme created by Miah with assistance from hyprnova