程序抛出异常 [英] Program throwing exception
问题描述
我正在编写一个程序,允许用户输入一个句子,然后存储在一个字符串中,然后程序将删除句子中的所有句子,然后在输出之前创建字符串中每个单词的列表列表到控制台。
I am writing a program that allows the user to enter a sentence which then gets stored in a string and then the program will remove any full stops in the sentence and then create a list of each individual word in the string before outputting that list to the console.
程序运行正常,只要句子中只有一个完整句,但如果有任何更多,它抛出此异常:
the program is working perfectly as long as there is only 1 full stop in the sentence but if there are any more than that it throws this exception:
在Project6.exe的0x7696B727处的未处理异常:Microsoft C ++异常:std :: out_of_range在内存位置0x0022F8B8。
Unhandled exception at at 0x7696B727 in Project6.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0022F8B8.
然后如果我继续运行它它会抛出:
and then if I continue to run it it throws:
运行时检查失败#0 - ESP的值没有正确保存在一个函数调用。这通常是调用用一个调用约定声明的函数的结果,该函数指针用不同的调用约定声明。
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
任何建议? (在任何人问之前,我知道你通常只有1句全句,但我需要做超过1作为测试的一部分。
any suggestions? (and before anyone asks, i know you usually only have 1 full stop in a sentence but i need to do it with more than 1 as part of testing.
这里是代码:
#include <iostream>
#include <string>
using namespace std
string sSentence; // sets up a string variable called sSentence
int i = 0;
void RemoveFullStop()
{
while(sSentence.find (".") != string::npos) // the program runs this loop until it cannot find any more full stops in the string
{
i = sSentence.find("." , i); // find the next full stop in the string and get the character count of the space and place it in the variable i
sSentence.replace(i,1,""); // remove the full stop
}
}
void ListWords()
{
while(sSentence.find (" ") != string::npos) // the program runs this loop until it cannot find any more spaces in the string
{
i = sSentence.find(" " , i); // find the next space in the string and get the character count of the space and place it in the variable i
// cout << i << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used)
sSentence.replace(i,1,"\n");
// cout << sSentence << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used)
}
}
int main()
{
getline(cin, sSentence); // get user input and store it in sSentence (using the getline function so the .find operation works correctly)
RemoveFullStop(); // calls the RemoveFullStop void function that removes all full stops from the string
ListWords(); // calls the ListWords void function that splits the string into a list of words
cout << endl; // formatting line
cout << "The words that were in the sentence were:" << endl;
cout << endl; // formatting line
cout << sSentence << endl;
cout << endl; // formatting line
system("pause");
return 0;
}
推荐答案
当ListWords运行中 sSentence.find(,i)
时,i的值不为0,因为它已在 RemoveFullStop c $ c>。要解决此问题,请先删除
int i = 0;
,然后将其添加到 RemoveFullStop()
和 ListWords()
此外,虽然这只是一个风格的东西,不会影响你的代码运行能力,我不会调用这个变量 i
作为 i,j,k
通常意味着计数器。
The reason this happens is that when sSentence.find(" " , i)
in ListWords runs, i's value is not 0 because it was already defined in RemoveFullStop()
. To fix this, first remove int i = 0;
, then add it to RemoveFullStop()
and ListWords()
Also, while this is just a style thing and won't effect your codes ability to run, I wouldn't call this variable i
as i,j,k
usually imply counters. Call this variable something more appropriately descriptive.
这里是相关的代码,因为它应该是。
Here is the relevant code as it should be.
using namespace std
string sSentence;
void RemoveFullStop()
{
int charPlace = 0;
while(sSentence.find (".") != string::npos)
{
charPlace = sSentence.find("." , charPlace);
sSentence.replace(charPlace,1,"");
}
}
void ListWords()
{
int charPlace = 0;
while(sSentence.find (" ") != string::npos)
{
charPlace = sSentence.find(" " , charPlace);
sSentence.replace(charPlace,1,"\n");
}
}
这篇关于程序抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!