Print enum value

This is a discussion on Print enum value within the C Programming forums, part of the General Programming Boards category; I'm trying to figure out how to print an enum variable name verses the enum value. currently my code looks ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    Print enum value

    I'm trying to figure out how to print an enum variable name verses the enum value. currently my code looks something like this. It prints the numeral 0-2 verses the enum that corresponds with the value.

    Code:
    case win:
    			++*win_cnt_ptr;
    			//printf("%27sYou win.\n", "");
    			printf("%s%d%s%d%s\n", "You chose ", player_choice, 
    			" and I chose ", machine_choice, " You win.\n");
    			break;
    When this prints, the following text is printed:

    You chose 2 and I choose 1 You win.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    create an array of strings that contain the names you want, then use player_choice and machine_choice as the index into the string array.
    Code:
    char* names[] = {"One","Two","Three", ...}
    
    
    printf("you chose %s\n", names[player_choice]);

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    typedef enum
    {  one,
       two,
       three
    } myenum;
    
    
    int main()
    {
    	char test[3][6]={"one","two", "three"};
    	myenum value=two;
    	char tmp[6]={0x0};
    	
    	strcpy(tmp,test[(int)value]);
    	printf("%s\n", tmp);
    	return 0;
    }

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdio.h>
    #define X(x) #x
    
    int main(void){
      int i = 9432;
      printf("%s = %d",X(i),i);
      return 0;  
    }
    (I don't think I read the question properly...)
    Last edited by sand_man; 09-08-2005 at 07:21 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    I had this fixed by doing the following:

    Code:
    case win:
    			++*win_cnt_ptr;
    			//printf("%27sYou win.\n", ""); //old code
    			printf("%s%s%s%s%s\n", "You chose ", names[player_choice], 
    			" and I chose ", names[machine_choice], " You win.\n");
    			break;
    It was working sort of, except the logic wasnt quite right. Now when I went back to fix that, I seemed to have broken this. Ive been trying for the last two days to fix this and havent gotten anywhere. It will print the dec equiv but if I try to invoke printing the name, it crashes. When I take out the new code, the program still works.

    Here is the response of the debugger if it helps.
    Code:
    >	Ch 7 Ex 5.exe!report(outcome result=win, int * win_cnt_ptr=0x0012fed4, int * lose_cnt_ptr=0x0012fec8, int * tie_cnt_ptr=0x0012febc)  Line 16 + 0x11	C

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    How is names declared? Are you trying to access beyond the end of the array?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by Ancient Dragon
    create an array of strings that contain the names you want, then use player_choice and machine_choice as the index into the string array.
    Code:
    char* names[] = {"One","Two","Three", ...}
    
    
    printf("you chose %s\n", names[player_choice]);
    Don't forget an array index starts at zero. In this example, if the user enters the value 1, the program will print "you chose Two". So, make names:
    char* names[] = {"Zero", "One","Two","Three", ...}
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    I'm going to post the whole section of code. The program itself spreads itself over 5 files. The logic I used was to return the players_choice and machine_choice to report.c. The problem seems to be that my print statements no longer print the enum value 0, 1, or 2. I get a number 8-10 digit integer number instead. Its like they are calling something else. Its quite frustrating.

    Code:
    #include "p_r_s.h"
    
    void report(outcome result, int *win_cnt_ptr, 
    			int *lose_cnt_ptr, int *tie_cnt_ptr, p_r_s player_choice, p_r_s machine_choice)
    {
    
    // New code added to address arrary to variable name conversion
    char* names[] = {"paper","rock","scissors"};
    // End new code	
    
    	switch (result) {
    		case win:
    			++*win_cnt_ptr;
    			//printf("%27sYou win.\n", "");
    			printf("%s%d%s%d%s\n", "You chose ", player_choice, 
    			" and I chose ", machine_choice, " You win.\n");
    			break;
    		case lose:
    			++*lose_cnt_ptr;
    			// New code
    			//printf("%s%s%s%s%s\n", "You chose ", names[player_choice], 
    			//" and I chose ", names[machine_choice], " You lose.\n");
    			// End new code
    			
    			printf("%27sYou lose.\n", ""); // Old Code
    			break;
    		case tie:
    			++*tie_cnt_ptr;
    			
    			// New Code
    			//printf("%s%s%s%s%s\n", "You chose ", names[player_choice], 
    			//" and I chose ", names[machine_choice], " A tie.\n");
    			// End new code
    
    			printf("%27sA tie.\n", ""); // Old code
    			break;
    		default:
    			printf("PROGRAMMER ERROR:  Unexpected result!\n\n");
    			exit(1);
    	}
    }
    Last edited by kryonik; 09-13-2005 at 06:17 AM.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try this:
    Code:
    #include <stdio.h>
    
    typedef enum
    {
      WIN,
      LOSE,
      TIE
    } outcome;
    
    typedef enum
    {
      CHOICE_P,
      CHOICE_R,
      CHOICE_S
    } p_r_s;
    
    void report(outcome result, p_r_s player_choice, p_r_s machine_choice)
    {
      char  *names[] = { "paper",
            "rock",
            "scissors" };
    
      char  *w_l_t[] = { "You win",
            "I win",
            "A tie" };
    
      printf("You chose %s, I chose %s, %s.", 
              names[player_choice], 
              names[machine_choice], 
              w_l_t[result]);
    }
    
    int main(void)
    {
      report(WIN, CHOICE_P, CHOICE_R);
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print enum field literals
    By patiobarbecue in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2009, 08:48 AM
  2. how do you print a typedef enum?
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 08:50 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM

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