Hello,
I would like to know if there is a function which converts a letter to a number.. A to 1, B to 2 ... Z to 26...
Hello,
I would like to know if there is a function which converts a letter to a number.. A to 1, B to 2 ... Z to 26...
There will be one after you write it
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok.. Thanks!
Or you could use enums if you are interested in a function.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Erm, what have enumerations to do with functions?
Bye, Andreas
Oh Andreas good thing you pointed that out! I wanted to write "if you are not interested in using a function. Thanks.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Good idea! Σε ευχαριστώ!
I need a guide, something, for enumerators! thanks!
Well when I need a quick zipped reminder on enums I take a look here. Of course, if you google you can find more. You can always ask if needed.
PS - Παρακαλώ
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
What I want to do, is to get a char type variable from the keyboard and compare it with the enum? Is that possible?
Yes. Start the enum's first letter (A) with the corresponding ASCII value... that way you will get all the letters the correct ASCII value. Then read normally from input and just compare like thisand I think that would be enoughCode:if(c==A)
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Ok, but is possible to compare the char type variable with the enum at once? I mean, can I treat the enumerator as an array?
For example:
Or somehow else?Code:enum converter {A = 65, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z}; for (k = 0; k < 26; k++) if (((var = strcmp(input[k], converter[k]))) == 0) x = 'converter[k]' - 64;
Last edited by Sotiris Kaniras; 03-05-2013 at 05:05 AM.
Are you guys sure that enums will work? AFAIK, in C it is not guaranteed that an enum is implicitly convertible to an integral type. Considering char is an integral type, this is important.
Well, Just to be clear, you don't need enums, at all.
Even editing your own answer is close enough:
Assuming input is a char...Code:#include <ctype.h> /* provides tolower() */ for (k = 0; k < 26; k++) if (tolower(input) == "abcdefghijklmnopqrstuvwxyz"[k]) return k;