Convert String into integer
Turn String a into integer x. a can be a letter from A to J. I' having trouble just making if statements like this.
1 2 3 4 5 6 7 8 9 10 11 12
|
string a;
int x;
if (a[0] = 'A')
{
cout << endl << "It's A" << endl;
x = 0;
}
else if (a[0] = 'B')
{
cout << endl << "It's B" << endl;
x = 1;
}
|
I thought I could also make a switch statement but string a would need to be an integer.
try this:
Your first problem is that you are using
if (a[0] = 'A')]
instead of
if (a[0] == 'A')
Secondly, a massive chain of if, else if statements means that there is certainly a better way to do this.
'A' is the ascii representation for the numeric value 0x41. Try this:
1 2 3 4
|
int char2int(char c)
{
return (int)(c - 0x41);
}
|
or
1 2 3 4
|
int char2int(char c)
{
return (int)(c - 'A');
}
|
http://www.asciitable.com/
Topic archived. No new replies allowed.