c++ - 输入7个字母单词,输出7位数电话号码

标签 c++ string error-handling c++14

我正在为作业编写代码。我输入了7个字母,并在小键盘上输出了相应的数字。到目前为止,它可以很好地转换字母,它会忽略我想要的第七个字母之后的所有内容,并在您键入stop时停止。但是,如果我包含空格(这是分配所必需的),它将引发错误,我写了一些应该使它忽略它们但不能正常工作的东西。另外,如果我输入的内容少于7个字母,我也会收到错误消息。 “在抛出'std::out_of_range'what()实例后调用终止:basic_string::at:_n(为1)> = this-> size()(为1)已中止(转储核心)
我很确定我得到的是因为while循环,在调试器中,它总是在那里停止,但是我不知道该如何解决。我期待收到任何人的来信。先感谢您!

#include <iostream>
#include <string>

using namespace std;

int main(){
    //declares variables
    string str1, str2;
    int i;
    //first input
    cout << "Type 'stop' to stop or type your 7 letter message to be converted to numbers: " << endl;
    cin >> str1;
    
    //while loop so that you can input multiple numbers one after the other until stop is read
    while (str1 != "stop"){
        //resets i if str1 does not equal stop
        i=0;
        while(str2.length() != 7){
            //determines what number to assign to the charcter at the index of the i variable
            if (str1.at(i) == 'a' || str1.at(i) == 'A' || str1.at(i) == 'b' || str1.at(i) == 'B' || str1.at(i) == 'c' || str1.at(i) == 'C'){
                str2 = str2 + '2';
                i=i+1;
            }else if (str1.at(i) == 'd' || str1.at(i) == 'D' || str1.at(i) == 'e' || str1.at(i) == 'E' || str1.at(i) == 'f' || str1.at(i) == 'F'){
                str2 = str2 + '3';
                i=i+1;
            }else if (str1.at(i) == 'g' || str1.at(i) == 'G' || str1.at(i) == 'h' || str1.at(i) == 'H' || str1.at(i) == 'i' || str1.at(i) == 'I'){
                str2 = str2 + '4';
                i=i+1;
            }else if (str1.at(i) == 'j' || str1.at(i) == 'J' || str1.at(i) == 'k' || str1.at(i) == 'K' || str1.at(i) == 'l' || str1.at(i) == 'L'){
                str2 = str2 + '5';
                i=i+1;
            }else if (str1.at(i) == 'm' || str1.at(i) == 'M' || str1.at(i) == 'n' || str1.at(i) == 'N' || str1.at(i) == 'o' || str1.at(i) == 'O'){
                str2 = str2 + '6';
                i=i+1;
            }else if (str1.at(i) == 'p' || str1.at(i) == 'P' || str1.at(i) == 'q' || str1.at(i) == 'Q' || str1.at(i) == 'r' || str1.at(i) == 'R'|| str1.at(i) == 's' || str1.at(i) == 'S'){
                str2 = str2 + '7';
                i=i+1;
            }else if (str1.at(i) == 't' || str1.at(i) == 'T' || str1.at(i) == 'u' || str1.at(i) == 'U' || str1.at(i) == 'v' || str1.at(i) == 'V'){
                str2 = str2 + '8';
                i=i+1;
            }else if (str1.at(i) == 'w' || str1.at(i) == 'W' || str1.at(i) == 'x' || str1.at(i) == 'X' || str1.at(i) == 'y' || str1.at(i) == 'Y'|| str1.at(i) == 'z' || str1.at(i) == 'Z'){
                str2 = str2 + '9';
                i=i+1;
            }else if(str1.at(i) == ' '){
                //if there is a space, it is ignored
                i=i+1;
            }else{
                //in case they put somthing weird, sets str2 to length of 7 to end while
                cout << "incorrect input";
                str2 = "abcdefg";
            }
        }
        //outputs converted number
        cout << str2.substr(0,3) << "-" << str2.substr(3,7) << endl << endl;
        cout << "Type 'stop' to stop or type your 7 letter message to be converted to numbers: " << endl;
        cin >> str1;
        //resets str2
        str2 = "";
        
    }
    return 0;
}

最佳答案

  • 即使其中包含空格字符,也应使用 std::getline() 读取整行。
  • 您应检查输入字符串的长度,并在字符串末尾停止,以避免std::out_of_range错误。
  • #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
        //declares variables
        string str1, str2;
        int i;
        //first input
        cout << "Type 'stop' to stop or type your 7 letter message to be converted to numbers: " << endl;
        getline(cin, str1); // *** use std::getline()
        
        //while loop so that you can input multiple numbers one after the other until stop is read
        while (str1 != "stop"){
            //resets i if str1 does not equal stop
            i=0;
            while(str2.length() != 7){
                //determines what number to assign to the charcter at the index of the i variable
                if (i >= str1.length()) { // *** length check
                    cout << "input too short";
                    str2 = "abcdefg";
                }else if (str1.at(i) == 'a' || str1.at(i) == 'A' || str1.at(i) == 'b' || str1.at(i) == 'B' || str1.at(i) == 'c' || str1.at(i) == 'C'){
                    str2 = str2 + '2';
                    i=i+1;
                // *** omit ***
                }else{
                    //in case they put somthing weird, sets str2 to length of 7 to end while
                    cout << "incorrect input";
                    str2 = "abcdefg";
                }
            }
            //outputs converted number
            cout << str2.substr(0,3) << "-" << str2.substr(3,7) << endl << endl;
            cout << "Type 'stop' to stop or type your 7 letter message to be converted to numbers: " << endl;
            getline(cin, str1); // *** use std::getline()
            //resets str2
            str2 = "";
            
        }
        return 0;
    }
    

    关于c++ - 输入7个字母单词,输出7位数电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65545126/

    相关文章:

    c++ - 正则表达式算法 - 组合或

    python - 如何连接字符串计数和 Int

    sockets - 如何从 os.Error 获取 os.Errno?其他使用 os.Timeout 的方法?

    php - Laravel-多种形式的错误

    html - 在 div 屏幕上打印 console.error 日志

    c++ - 命名空间 ‘function’ 中的 ‘std’ 没有命名类型

    c++ - 如何在 Xcode 中引用图像和其他对象以及将它们放置在何处?

    c++ - GLFW 无法在 Mavericks 上获得 OpenGL 4.1 上下文

    通过将字符串转换为日期格式来对 Javascript 日期进行排序

    java - 将 token 传递给java中的函数