c++ - 从字母转换成单词

标签 c++ string debugging assertion

总的来说,这是作业的一小部分,但这个函数给我一个错误“调试断言失败!”

void English_to_SMS(void)
{
    int i;
    bool vowel;
    string word;
    string letter;

    cout << "Enter y, n or m: ";
    cin >> word;
    for (int i = 0; i <= word.length(); ++i)
    {
        letter = word[i];
        if (letter == "y") {
        cout << "yes";
        }
        else if (letter == "n") {
            cout << "no";
        }
        else if (letter == "m") {
            cout << "maybe";
        }
        else
        {
            cout << letter ;
    }
}

错误信息我截图了:http://i.imgur.com/maQN7Vs.png

错误说:

Debug Assertion Failed!

Program: ..ects]English.SMS_Translator\Debug\English._SMS_Translator.exe File:e:\microsoft visual studio 10.0\vc\include\xstring Line:1440

Expression: string subscript out of range

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press retry to debug the application)

对于解决此错误的任何帮助或建议,我们将不胜感激。

最佳答案

for (int i = 0; i <= word.length(); ++i)

应该是

for (int i = 0; i < word.length(); ++i)

N 大小容器的下标范围从 0 到 N-1

关于c++ - 从字母转换成单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843319/

相关文章:

c++ - 除了提供必要的保证之外,硬件内存屏障还可以使原子操作的可见性更快吗?

javascript - 如何在 Javascript 中 append 或连接字符串?

java - 使用 for 循环创建变量

java - matches() 匹配Java中必须以大写字母开头的单词

c++ - 在 C++ 中打开/关闭我自己的日志(cout)打印——怎么做?有什么特别的选择吗?

用于打印函数执行的每一行的 Python Decorator

javascript - Firefox 47 中的 "pause on exceptions"在哪里?

c++ - 从用作数据部分的字符串派生的类

c++ - QTextBrowser 设置链接,无法使其可点击

c++ - QT/QML renderig - 质量差