Using Switch to Convert Char to Int

Hello All, I am trying to convert a series of letters in char(not string) to numbers. I have setup the switch case but it doesnt seems to work. If someone can take a look and give me a hint I would really appreciate it.
Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
                        char suit;

                       switch (suit)
			{
			case 'p':{

			   int p = 0;


			}
				break;

			case 't':{
				int t = 1;
			}
				break;

			case 'c':{
				int c = 2);
			}
				break;

			case 'd':{
				int d = 3;
			}
				break;
				
			}
line 20: remove the closing parenthesis

i dont see any convertion in your code.

it should be , example,
1
2
3
case 'p':
{  int p = suit; }
break;



try look at ascii code in this sites tutorial
Hello, but why would it be int p = suit, if what I want is int p = 0 (the number 0)
you mean character 'p' will convert into 0?
yes thats what I mean, I found this on ascii:


public class char_to_int
{
public static void main(String args[])
{
char myChar = 'a';
int i = (int) myChar; // cast from a char to an int

System.out.println ("ASCII value - " + i);
}
}

but it seems too complicated to be the solution. Thank you!
here is what I´ve come up with acii:

char p = 48;
int i = (int)p;
(48 - +i);
what you found is a java code.


1
2
3
case 'p':
{  int p = suit; }
break;


this really should do it, you assign the character into an int variable and it will convert it.
using switch just like your code doesnt make any sense.

EDIT:
in your code, suit holds the characters.
in ascii, character 'p' is 112 in int
Last edited on
If you want p, t, c, and d to map to 0, 1, 2, and 3 then just use a c-string:
int charToInt(char suit)
{
static const char *map="ptcd";
const char *where = strchr(map, p);
return (where ? where-map : -1);
}

You could use a std::string also but this will save a few bytes because it won't copy the string to the heap.
Topic archived. No new replies allowed.