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; good morning As you see in my code source I ceated the enum boolean but the problem is when I ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Question how to scan a var in enum

    good morning
    As you see in my code source
    I ceated the enum boolean
    but the problem is when I want to scan "biblio[i].emprunter"
    what should i use ?
    scanf or gets or what ??
    and If I must use scanf what should I use whit it ? "%d'' or "%s" ...etc
    Code:
    #include <stdio.h>
    typedef struct fiche fiche;
    typedef enum Booleen Booleen ;
    typedef enum Categorie Categorie ; 
        enum Booleen {vrai,faux};
        enum Categorie {Conte=0,Policier=1,Roman=2,Histoire=3};
        struct fiche    
        {
            int cote ;
            char titre [100];    
            Categorie categorie;        
            Booleen emprunter ;
            int annee ;
        };
    main()
    {
        FILE* bib;
        int nblivre,i;
        fiche biblio[500];
    /************************************************************************************************************/
     
            scanf("%d",&biblio[i].emprunter);
            printf("Donnez l'annee de l'edition :");
            getchar();
            scanf("%d",&biblio[i].annee);
        }
    I'm waitting for your help
    and thank you !
    Last edited by med linux; 03-24-2011 at 04:49 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What did we tell you about weird fonts?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    What did we tell you about weird fonts?
    ok ... I'm sorry

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    First off, whatever you do, don't use gets! It's horribly dangerous. Use fgets on stdin instead.

    Also, there is no scanf format specifically for enums. What do you want the user to type in for emprunter? Do you want them to type a 0 or a 1? If so, use %d, but make sure you check that they don't input something like a 2, since that isn't a valid enum value. If you want them to type a string, like "oui" or "non", use %s.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    look I changed my code but It won't work
    Code:
    #include <stdio.h>
    
    /***************************************************************************************************************************************************************/
    typedef struct fiche fiche;
    typedef enum Booleen Booleen ;
    typedef enum Categorie Categorie ; 
    
    	enum Booleen {vrai,faux};
    	enum Categorie {Conte=0,Policier=1,Roman=2,Histoire=3};
    	struct fiche	
    	{
    		int cote ;
    		char titre [100];	
    		Categorie categorie;		
    		Booleen emprunter ;
    		int annee ;
    	};
    
    		scanf("%s",&biblio[i].emprunter);
    		printf("Donnez l'annee de l'edition :");
    		getchar();
    		scanf("%d",&biblio[i].annee);
    	}
    Last edited by med linux; 03-24-2011 at 04:49 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Please don't center your text unless you're writing a poem.

    Does your compiler give warnings when you compile that code? It it does, pay attention to them and fix them. If not, turn the warnings up all the way, and if that doesn't work, get a new compiler.

    You can't scan a string into an enum. An enum is like an integer. Scan into a temporary string and check that string:
    Code:
    char string[50];  // temporary string to hold user input
    
    scanf("%49s", string);  // don't overflow the string
    
    if (strcmp(string, "oui") == 0)
        biblio[i].emprunter = vrai;
    else if...
    The same goes for your print statement. emprunter works like a integer, so you can't print it with %s. You need to convert that enum back into a string:
    Code:
    fprintf(bib, "emprunter : %s \n", biblio[i].emprunter == vrai ? "oui" : "non");

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    oh .. the same problem ...
    Code:
    #include <stdio.h>
    
    /***************************************************************************************************************************************************************/
    typedef struct fiche fiche;
    typedef enum Booleen Booleen ;
    typedef enum Categorie Categorie ; 
    /***************************************************************************************************************************************************************/
    	enum Booleen {faux,vrai};
    	enum Categorie {Conte=0,Policier=1,Roman=2,Histoire=3};
    	struct fiche	
    	{
    		int cote ;
    		char titre [100];	
    		Categorie categorie;		
    		Booleen emprunter ;
    		int annee ;
    	};
    		printf("le livre est emprunte ou non ? ");
    		getchar();
    		gets(rep);
    		if (strcmp(rep,"oui")==0)
    		{
    			biblio[i].emprunter=vrai;
    		}
    		else
    		{
    			biblio[i].emprunter=faux;
    		}
    		printf("Donnez l'annee de l'edition :");
    		getchar();
    		scanf("%d",&biblio[i].annee);
    	}
    
    sauvgarder(biblio,nblivre);
    
    }
    Last edited by med linux; 03-24-2011 at 04:50 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    You keep using getchar() all over the place and it's reading legitimate input. gets() doesn't leave the new line in the buffer, so you don't need those getchar() calls. Also, you didn't follow my suggestion for the fprintf call that prints emprunter.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    You can't use scanf with enums (enumerated constants). I would say enum is the wrong type to use to begin with anyway. Where you use enum, you may as well use int since the input is variable, and self-impose your own range like [0, 3].

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you insist on using enums, make a function that translates for you:
    Code:
    enum foo myfunc( int input )
    {
        switch( input )
        {
            case 1: return enum_one;
            ...
        }
        return enum_error;
    }
    Then scanf into an integer and pass it to this function to get the enum you have associated with it.

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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by med linux View Post
    anduril462 could you (please) edit my code ?
    What exactly are you trying to accomplish here?

    An enum simply allows you to use names for values...
    Code:
    	enum Categorie {Conte=0,Policier=1,Roman=2,Histoire=3};
    
    if (Histoire)
     {... }
    But the reverse does not apply... You cannot stuf in a number and get the name out. Doesn't work that way.

    Enums are also constants... once set they cannot be changed at run time.

    So, forget trying to add to it, back into it, or whatever it is you're trying to do....

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    I will give you a simple exemple
    look
    Code:
    #include <stdio.h>
    typedef enum Booleen Booleen ;
    	enum Booleen {faux,vrai};
    main ()
    {
    booleen b ;
    scanf("%d",&b);
    }
    how to print "vrai" or "faux"
    only with printf or put ?
    Last edited by med linux; 03-24-2011 at 03:19 PM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by med linux View Post
    this is it i want to get names
    how to do it ?
    You build a 2d character array containing the words....
    Code:
    char words[5][20] = {"Fred", "Martha", "John", "Alice","Davie"}
    
    int x = 3;
    
    Printf("%s", words[x]);
    In future, rather than wasting everyone's time and effort, please just come straight out and tell us what you're trying to do. "Please edit my code" is so far beyond lame...

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Quote Originally Posted by med linux View Post
    I will give you a simple exemple
    look
    Code:
    #include <stdio.h>
    typedef enum Booleen Booleen ;
    	enum Booleen {faux,vrai};
    main ()
    {
    booleen b ;
    scanf("%d",&b);
    }
    how to print "vrai" or "faux"
    only with printf or put ?
    Look at the last bit of code in my post #6. I showed you exactly what to do to your printf to make it work.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    look
    Code:
    #include <stdio.h>
    typedef enum Booleen Booleen ;
    	enum Booleen {faux,vrai};
    main ()
    {
    Booleen b ;
    scanf("%s",&b);
    if (strcmp(b,"vrai")==0)
    {printf ("salut");}
    }
    it won't work again !

Page 1 of 2 12 LastLast
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