0

poziomy= char; pionowy= digit; ( no problems with this one)

So I need to convert char into a digit in function but obviusly I cannot do char=int, so I dont know how to pass on the converted char into digit properly.

I guees i can do two functions but maybe there is an easier way?

I thought of making a new variable poziomy_c but I dont know how to pass it to Ruch_gracza()

int Convert_digit (int cyfra) { switch (cyfra) { case 10: return 0;break; case 9: return 1;break; case 8: return 2;break; case 7: return 3;break; case 6: return 4;break; case 5: return 5;break; case 4: return 6;break; case 3: return 7;break; case 2: return 8;break; case 1: return 9;break; } } int Convert_letter (char literka) { switch (literka) { case 'A': return 0; break; case 'B': return 1; break; case 'C': return 2; break; case 'D': return 3; break; case 'E': return 4; break; case 'F': return 5; break; case 'G': return 6; break; case 'H': return 7; break; case 'I': return 8; break; case 'J': return 9; break; } } void Conwert(int &pionowy, char poziomy) { pionowy=Convert_digit(pionowy); int poziomy_c; poziomy_c=Convert_letter (poziomy); } void Ruch_gracza1 (int plansza[10][10]) { int pionowy ; char poziomy; cout << "wprowadz wspolrzedne pola na ktorym lezy pion który chcesz ruszyc ( w pionie , potem w poziomie)" << endl; cin >> pionowy >> poziomy; Conwert (pionowy,poziomy); cout << pionowy << endl; cout << poziomy << endl; } 
6
  • If you have a char variable ch which contains a digit from '0' to '9' then simply ch - '0' is the integer value of that digit. Is that what you wanted? It's a bit hard to tell from your question. Commented Dec 2, 2020 at 13:02
  • If you post code here you should write the code with english variable and function names and text. One important part for understanding the code are variable and function names. Commented Dec 2, 2020 at 13:03
  • A c-type conversion will do that: char b; funtion((int) b) <== this converses the char b to interger. A char is indeed a 1-byte integer. Commented Dec 2, 2020 at 13:04
  • 1
    @t.niese I think the policy here is that variable names can be in any ASCII-friendly language as long as the question title and body are in English: meta.stackoverflow.com/a/297680/2602718 Commented Dec 2, 2020 at 13:05
  • Very unclear. Is the issue that you don't pass 2nd param by reference: void Conwert(int &pionowy, char poziomy)? Commented Dec 2, 2020 at 13:06

3 Answers 3

5

You can use char arithmetic to make this a whole lot easier. Since 'A' to 'Z' will be contiguous in ASCII/Unicode, you can do literka - 'A' to get how far literka is from A (which is what your switch is doing):

int Convert_letter (char literka) { if(!std::isalpha(literka)) { return literka; } // Not a letter return std::toupper(literka) - 'A'; } 

Or if you want a more robust solution to cover even less common character encodings:

int Convert_letter (char literka) { if(!std::isalpha(literka)) { return literka; } // Not a letter std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; return std::distance(std::begin(alphabet), std::find(std::begin(alphabet), std::end(alphabet), literka));; } 

Convert_digit will look similar (except with std::isdigit instead of std::isalpha).

Sign up to request clarification or add additional context in comments.

4 Comments

And for EBCDIC?
@Jarod Do you have a suggestion for a more portable solution for such a system?
std::distance(std::begin(alphabet), std::find(std::begin(alphabet), std::end(alphabet), letter));.
Now to continue in nit-picking, isapha has bad interface, and require to be wrapped :/ and it is also locale dependent.
0

You can do as

char c = 'B'; int digit = c - 'A'; return digit; 

Comments

0

You need some knowledge about the ASCII table and data type in C++. Simply, a char is an integer from -128 ... 127. If you declare a char variable name ch like this:

char ch = 'B'; 

C++ will understand that ch = 66 (look at ASCII table). So that we can do arithmetic operator with ch like an integer variable.

ch - 'A'; // result 1, because 'A' = 65 ch - 65; // same result with ch - 'A' 

Finally, you can write your function like this:

int functionChar2Int(char x){ return x - 'A'; } 

3 Comments

"a char is an integer from -128 ... 127". No, char might be signed or not, so only 0-127 range is guaranteed.
ASCII is not the only mapping, there exist EBCDIC for example, and ch - 'A' won't give expected result.
Thank you for your comment about EBCDIC.