数组输出不正确-C++; 我正在编写一个C++控制台应用程序,我把 A变成 1 、代码> B到 2 >代码>等等。问题是,它输出的是48和52这样的数字——尽管我所基于的数组只有26个

数组输出不正确-C++; 我正在编写一个C++控制台应用程序,我把 A变成 1 、代码> B到 2 >代码>等等。问题是,它输出的是48和52这样的数字——尽管我所基于的数组只有26个,c++,C++,代码如下: void calculateOutput() { while (input[checkedNum] != alphabet[checkedAlpha]) { checkedAlpha++; if (checkedAlpha > 27) { checkedAlpha = 0; } } if (input[checkedNum] == alphabet[checkedAlpha])

代码如下:

void calculateOutput() {

    while (input[checkedNum] != alphabet[checkedAlpha]) {
        checkedAlpha++;
        if (checkedAlpha > 27) {
            checkedAlpha = 0;
        }
    }

    if (input[checkedNum] == alphabet[checkedAlpha]) {
        cout << numbers[checkedAlpha] << "-";
        checkedAlpha = 0;
        checkedNum++;
        calculateOutput();
    }
}

它的
int
数组意味着它将保存字符的ASCII值

如果你仔细观察,你会发现48,49,50,。。。是数字0,1,2

您需要做的是扣除表->'0'(48)中第一个数字的值

顺便说一句,这里有一些提示,让你更容易

tolower(inputAlphabet[index]) - 'a' + 1  // For 'a' output is 1 and so on :-)

获取字母表中字母数(i9ndex)的算法非常简单。不需要表格,简单的减法就可以了

int getLetterIndex(char c)
{
    // returns -1 if c is not a letter
    if ('a' <= c && c <= 'z')
        return 1 + c - 'a';
    if ('A' <= c && c <= 'Z')
        return 1 + c - 'A';
    return -1; 
} 
int getLetterIndex(字符c)
{
//如果c不是字母,则返回-1

如果('a'如何定义
输入
字母表
,和
数字
提示:
输入框-'a'+1
将提供您所需的内容
数字
数组来自何处?不管您的输出(比如48等等)听起来像。@plezhelpmeh请用添加的数据编辑问题…Offfish主题:我很高兴它对您有所帮助,记住ascii表。:-)
 int numbers [27] = { 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​,0 };
tolower(inputAlphabet[index]) - 'a' + 1  // For 'a' output is 1 and so on :-)
int getLetterIndex(char c)
{
    // returns -1 if c is not a letter
    if ('a' <= c && c <= 'z')
        return 1 + c - 'a';
    if ('A' <= c && c <= 'Z')
        return 1 + c - 'A';
    return -1; 
}