c++ - for 循环异常 C++

标签 c++ for-loop runtime-error

<分区>

对于我的 C++ 类(class),我必须编写一个程序,该程序从用户那里获取一个包含字母的电话号码(例如 1-800-GOFEDEX),并返回该电话号码以及他们应该调用的实际号码。我在 for 循环中有我的转换逻辑:

    for (int i = 0; i <= 7; i++) {
    switch (letters[i]){
        case 'A':
        case 'B':
        case 'C':
            decodedNumber[i] = '2';
            continue;
        case 'D':
        case 'E':
        case 'F':
            decodedNumber[i] = '3';
            continue;
        case 'G':
        case 'H':
        case 'I':
            decodedNumber[i] = '4';
            continue;
        case 'J':
        case 'K':
        case 'L':
            decodedNumber[i] = '5';
            continue;
        case 'M':
        case 'N':
        case 'O':
            decodedNumber[i] = '6';
            continue;
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
            decodedNumber[i] = '7';
            continue;
        case 'T':
        case 'U':
        case 'V':
            decodedNumber[i] = '8';
            continue;
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            decodedNumber[i] = '9';
            continue;
        //the default will simply copy it over (in case there is a number mixed with the letters)
        default:
            decodedNumber[i] = i;
            continue;
    }
} //End of for loop

它编译得很好,但是当我运行它时,它会循环一次然后给我这个错误:

runtime error

我知道它至少经历了一次循环,因为您可以在这张图片中的 decodedNumber 变量的第一个位置看到数字 4:

Visual Studio During Debugging

我已经尝试在 switch 语句的所有情况下将 i 更改为 i+1,但这也不起作用。

非常感谢任何帮助!!!

Here's the entire program code以备不时之需

最佳答案

(一般性建议)

如果您将查找机制设置为(拨号盘描述):

include <map>

// Dial-pad description:
const std::map<char, unsigned int> lut{
    {'1',1},
    {'2',2}, {'A',2}, {'B',2}, {'C',2},
    {'3',3}, {'D',3}, {'E',3}, {'F',3},
    {'4',4}, {'G',4}, {'H',4}, {'I',4},
    {'5',5}, {'J',5}, {'K',5}, {'L',5},
    {'6',6}, {'M',6}, {'N',6}, {'O',6},
    {'7',7}, {'P',7}, {'Q',7}, {'R',7}, {'S',7},
    {'8',8}, {'T',8}, {'U',8}, {'V',8},
    {'9',9}, {'W',9}, {'X',9}, {'Y',9}, {'Z',9},
    {'0',0}
};

然后,代码变得简单:(并且具有一定程度的抽象)

#include <string>
#include <vector>

std::string Dial{"1-800-GOFEDEX"};
std::vector<unsigned int> Number;

for (auto & Ch : Dial) {
    auto it = lut.find(Ch);
    if (it != lut.end())
        Number.push_back(it->second);
}
  • 在这种情况下,您还可以在不修改代码的情况下添加对其他语言或其他拨号盘变体等的支持( map 初始化除外)。双语(第二语言0600-06FF取值范围)拨号盘,例如:

enter image description here

--

map lut 的替代方案(如果“ map 效率”是一个问题):

#include <array>
#include <climits>

std::array<unsigned int, 100> lut; // ASCII Code: '-' 45, '0'-'9': 48-57, 'A'-'Z': 65-90 
lut.fill(UINT_MAX);

// Dial-pad description:
lut['1'] = 1;
lut['2'] = lut['A'] = lut['B'] = lut['C'] = 2;
lut['3'] = lut['D'] = lut['E'] = lut['F'] = 3;
lut['4'] = lut['G'] = lut['H'] = lut['I'] = 4;
lut['5'] = lut['J'] = lut['K'] = lut['L'] = 5;
lut['6'] = lut['M'] = lut['N'] = lut['O'] = 6;
lut['7'] = lut['P'] = lut['Q'] = lut['R'] = lut['S'] = 7;
lut['8'] = lut['T'] = lut['U'] = lut['V'] = 8;
lut['9'] = lut['W'] = lut['X'] = lut['Y'] = lut['Z'] = 9;
lut['0'] = 0;

在这种情况下,代码如下所示:

#include <string>
#include <vector>

std::string Dial{"1-800-GOFEDEX"};
std::vector<unsigned int> Number;

for (auto & Ch : Dial)
    if (lut[Ch] < 10)
        Number.push_back(lut[Ch]);

关于c++ - for 循环异常 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663915/

相关文章:

c++ - 如何使用 C++20 istream_view?

c++ - 类中的 C++ 结构中的 Pragma

c++ - 如何链接到 Windows 7 64 位 Visual Studio 2012 中的 VTK 库?

c++ - 可移植 C++ 集成开发环境

scala - 如何展平使用 I/O 的嵌套 For Comprehension?

python - 对列表的列表求和?

Javascript : Omitted Initialize inside Nested For Loop

Android 致命信号 7 (SIGBUS),代码 1,故障地址 0xb8509ce1 in tid 32190 (com.example)

java - com.rest Api.java.jpa.UserDao Service CommandLineRunner 中的字段 userDAOService 需要类型为 'service.UserDAOService' 的 bean,但无法找到

python - 从 pd.DataFrame 设置 dtypes 给出 TypeError : object of type 'type' has no len()