how to scan a var in enum

This is a discussion on how to scan a var in enum within the C Programming forums, part of the General Programming Boards category; Why on earth would you think %s would work for an integer type? How many times do they have to ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why on earth would you think %s would work for an integer type? How many times do they have to tell you to stop scanning into an enum? Have you read anything we have written here?


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Booleen is an enum type! There isn't room to hold a whole string like "vrai" in there! We went over this already. You need a temporary string to store the users input, then use strcmp like so:
    Code:
    typedef enum Booleen Booleen ;
    enum Booleen {faux,vrai};
    
    int main (void)
    {
        Booleen b;
        char buf[50];
    
        scanf("%s", buf);
        if (strcmp(b, "vrai") == 0) {
            b = vrai;
        }
        else {
            b = faux;
        }
        printf("The value of b is %s\n", b == vrai ? "vrai" : "faux");
    
        return 0;
    }
    You have to translate the string the user types into the right enum value. scanf won't do it for you. You have to translate the enum value into a string to print. printf won't do it for you.

    Also, notice those two blue lines. It's int main(void) and return an int at the end (usually zero). Read about why this is important here: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    So I must use string ... ok

    thank you very much

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 01:32 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  4. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 12:12 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 08:24 AM

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