简体   繁体   中英

How to convert char into int in a function?

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;
}

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 ).

You can do as

char c = 'B';

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

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';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM