Thread: switch using enum

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    switch using enum

    Can someone please tell me why my output is always a 1 in my simple switch statement

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        enum switcher {off, on};
        char* state;
        switcher switch_state;
        
        cout << "Enter switch state:";
        cin >> state;   
    
        if (state == "off")
            switch_state = off;
        else
            switch_state = on;
            
        cout << switch_state << endl;
        system("PAUSE");
        return 0;
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    16
    Maybe you shouldn't declare state as a pointer, since the input is coming directly from the user. Though I might be wrong, my experience with enum is pretty green.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char* state;
    This is an unallocated pointer
    Try
    char state[100];

    > if (state == "off")
    This just compares pointers
    Try
    if ( strcmp( state, "off") == 0 )
    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.

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Cheers Salem

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    And as is often the case, just using the standard string class makes the fix even easier. Here is your original code using a string. All I did was switch the char* to string and add the <string> header. No need to use strcmp or set the size of the char array:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        enum switcher {off, on};
        string state;
        switcher switch_state;
        
        cout << "Enter switch state:";
        cin >> state;   
    
        if (state == "off")
            switch_state = off;
        else
            switch_state = on;
            
        cout << switch_state << endl;
        system("PAUSE");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM