Thread: type changing?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    type changing?

    if i have


    Student.h
    Code:
    class Student {
    public:
      enum Type { AStudent, BStudent, CStudent, DStudent };
      Type StudentType() const;
    private:
      Type myType;
    Student.cpp
    Code:
    Student::Type Student::StudentType() const {
            return myType;
    
    
    istream& Message::read(istream& stream) {
           int type;
           getline(stream,type); /*input should be a number [1-4]*/
      
           myType = type;    /* this does not work, complains about different types*/
           /*  how can i fix this? */
    
            return stream;
    }
    Last edited by paperbox005; 07-30-2004 at 03:36 AM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You need to convert the string to a Type yourself. For example, you could have a static map of strings to its corresponding Type enum, and then just lookup the string. Of course, since you only have four types, the easiest solution would probably be a series of if/else if statements checking the string against the appropriate text for each enum. If the string is "AStudent", then myType is AStudent, and so on.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    sorry i just realised i have an error in the post..

    the getline(stream,type)
    type should be of type int..
    so we are getting a integer [1-4] as input
    and so if its 2 then myType should be BStudent
    so how do i convert a number such as 2, into the string BStudent?
    thanks and sorry about that error

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Since enums start at 0, you will have to do a little conversion or specifically set AStudent = 1 in your enumeration. Assuming you leave the enum, you could do something like this:
    Code:
    if ((type-1) >= AStudent && (type-1) <= DStudent)
      myType = static_cast<Type>(type - 1);
    //else throw exception or handle error appropriately

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    class Student {
    public:
      enum Type { AStudent, BStudent, CStudent, DStudent };
      Type StudentType() const;
    private:
      Type myType;
    You could probably just change "myType" to an int, since there is an implicit conversion from enum types to int.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Since enums start at 0,
    Easy to fix
    Code:
    enum Type { AStudent = 1, BStudent, CStudent, DStudent };
    Now they start at 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM