Thread: Enum issue

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Enum issue


    hi guys,

    I'm kind of new in C programming..

    I'm trying to show a letter from "enum" acording to his index.

    I didn't find a way to do this ;\

    maybe u can figure out how to this..
    #include <stdio.h>

    int main(){
    int num_1, num_2, unNegativeNum, firstNum, X, Y;
    char capitalLetter, smallLetter;

    /*scan 2 numbers for the divide, a capital letter and an unnegative num.*/
    scanf("%d %d %c %d", &num_1, &num_2, &capitalLetter, &unNegativeNum);

    /*print the divide between the 2 num's without the remainder, the reminder, and the real result.*/
    printf("%d\n%d\n%.2f\n", num_1/num_2, num_1%num_2, num_1/(float)num_2);

    /*print the ASCI value of the capital letter.*/
    printf("%d\n", capitalLetter);

    /*inserting the ASCI value of the small letter.*/
    smallLetter = capitalLetter + 32;

    /*print the value of the small letter of the capital letter that we got from the user.*/
    printf("%c\n", smallLetter);

    /*finding the first in the order number and print it out.*/
    firstNum = unNegativeNum%10;
    printf("%c\n", (char)firstNum);

    /*deleting the first num by dividing it without reminder.*/
    unNegativeNum = unNegativeNum/10;

    /*finding the dozens number and putting the value in X.*/
    X = unNegativeNum%10;

    /*delting the dozens from the original numbers.*/
    unNegativeNum = unNegativeNum/10;

    /*input the hunderds value in Y*/
    Y = unNegativeNum%10;

    /*deleting the hunderds value from the original num'*/
    unNegativeNum = unNegativeNum/10;

    /*adding the thousand value into Y*/
    Y += unNegativeNum%10;

    /*building the dozens integer enum according to the assignment*/
    enum XLetter{A,B,C,D,E,F,G,H,I,J};

    /*building the 100' and the 1000' num' according to the assignment*/
    enum YLetter{z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i};

    enum XLetter x_value;
    enum YLetter y_value;


    printf("%s\n%s\n", x_value=X, y_value=Y );

    return 0;
    }
    the problem as u can see is in the last 6 lines..

    thnx.. ;)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by shokshok
    hi guys,

    the problem as u can see is in the last 6 lines..

    thnx.. ;)
    Yeah O.K., maybe you could elaborate what the problem is.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Do not post your code in quote tags. Use code tags.

    printf("%s\n%s\n", x_value=X, y_value=Y );
    That is nearly gibberish. The results of those assignment expressions (in 23 years of C, I've never before seen anyone attempt to use an assignment expression as a printf argument) are int, which you are telling printf to interpret as strings. I'm not quite sure what exactly printf will do, but the result will be garbage. I think it might be interpreting those int values as pointers to where the strings are, in which case it goes to some random location in memory and interprets whatever data there as characters until it just happens to hit a zero byte.

    enum's are basically identifiers for a sequence of integers starting with zero, unless you explicitly assign integer values to them. If you want to associate a particular string with an enum, the normal way would be to create an array of strings that you will then index with the enum. Another way might be to assign to the enum's their ASCII values and then printf an individual enum as a char (%c). I've used the former method and only just now thought of the latter.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    cheers..


    thank u very much! :)

IMN logo majestic logo threadwatch logo seochat tools logo