The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Enum issue
Discuss Enum issue in the C Programming forum on Dev Shed. Enum issue C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 6th, 2012, 06:48 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 33 m 36 sec
Reputation 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..
Quote: #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.. 
|

November 6th, 2012, 08:16 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
Quote: | 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.
|

November 6th, 2012, 09:00 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

November 6th, 2012, 10:39 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 33 m 36 sec
Reputation Power: 0
|
|
|
cheers..
thank u very much! 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|