总的来说,这是作业的一小部分,但这个函数给我一个错误“调试断言失败!”
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/