Thread: how to scan a var in enum

  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,909
    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

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, 02: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, 01:12 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM