尝试在C++;但不断得到错误R6010 void piglatin(字符串str) { string temp=str;//将传递给函数的字符串复制到temp中。 temp=temp+str.at(0);//将单词的第一个字符添加到单词的末尾。 temp=temp.erase(0,1);//从临时字符串中删除第一个字符。 temp=temp+“AY”;//将“AY”添加到临时字符串中 cout-sub; 拉丁语(sub); }while(iss); 我试图用这个方法在C++中分割一个字符串,但我仍然会得到一个错误,但是程序会执行我想做的事情。我只需要消除错误R6010。
您的代码基本上没有问题,只是您错误地检查了文件结尾(在本例中为字符串结尾),这导致将空字符串发送到尝试在C++;但不断得到错误R6010 void piglatin(字符串str) { string temp=str;//将传递给函数的字符串复制到temp中。 temp=temp+str.at(0);//将单词的第一个字符添加到单词的末尾。 temp=temp.erase(0,1);//从临时字符串中删除第一个字符。 temp=temp+“AY”;//将“AY”添加到临时字符串中 cout-sub; 拉丁语(sub); }while(iss); 我试图用这个方法在C++中分割一个字符串,但我仍然会得到一个错误,但是程序会执行我想做的事情。我只需要消除错误R6010。,c++,C++,您的代码基本上没有问题,只是您错误地检查了文件结尾(在本例中为字符串结尾),这导致将空字符串发送到piglatin(),导致str.at(0)异常 您可以通过以下方式解决此问题(包括使代码成为一个完整的可操作程序): void piglatin(string str) { string temp = str; //copies the string passed to the function into temp. temp = temp + str.at(0); //adds
piglatin()
,导致str.at(0)
异常
您可以通过以下方式解决此问题(包括使代码成为一个完整的可操作程序):
void piglatin(string str)
{
string temp = str; //copies the string passed to the function into temp.
temp = temp + str.at(0); //adds the first character of the word to the end of the word.
temp = temp.erase(0, 1); //deletes the first character from the temp string.
temp = temp + "AY"; //adds "AY" to the temp string
cout << temp << " "; //prints out the word followed by a space.
}
string userIn("I NEED THIS IN PIG LATIN");
istringstream iss(userIn);
do
{
string sub;
iss >> sub;
piglatin(sub);
} while (iss);
while (iss >> sub)
piglatin(sub);
我不认为这是完全正确的猪拉丁语(我似乎记得有些单词是以元音开头的,你必须把辅音组移到末尾,而不仅仅是第一个),但如果必要的话,我会让你去解决这个问题
就循环的工作方式而言:
IAY EEDNAY HISTAY NIAY IGPAY ATINLAY
这将一直持续到项目(本例中为单词)提取失败为止。天真的代码(我承认我犯过罪)会使用如下内容:
void piglatin(string str)
{
string temp = str; //copies the string passed to the function into temp.
temp = temp + str.at(0); //adds the first character of the word to the end of the word.
temp = temp.erase(0, 1); //deletes the first character from the temp string.
temp = temp + "AY"; //adds "AY" to the temp string
cout << temp << " "; //prints out the word followed by a space.
}
string userIn("I NEED THIS IN PIG LATIN");
istringstream iss(userIn);
do
{
string sub;
iss >> sub;
piglatin(sub);
} while (iss);
while (iss >> sub)
piglatin(sub);
但这并没有考虑到这样一个事实:即使提取失败,您也可能不在文件末尾,例如,如果您的短语末尾有空格,或者如果您正在扫描整数,而流中的下一个标记是非整数
另外,顺便说一句,不需要单独在
piglatin()
中执行所有这些操作,也不需要(显式)临时字符串。您可以用以下内容替换整个批次:
do {
string sub;
iss >> sub;
piglatin(sub);
} while (!iss.eof());
void piglatin(字符串str){
cout您的代码基本上没有问题,只是您错误地检查了文件结尾(在本例中是字符串结尾),这导致将空字符串发送到piglatin()
,从而导致str.at(0)
异常
您可以通过以下方式解决此问题(包括使代码成为一个完整的可操作程序):
void piglatin(string str)
{
string temp = str; //copies the string passed to the function into temp.
temp = temp + str.at(0); //adds the first character of the word to the end of the word.
temp = temp.erase(0, 1); //deletes the first character from the temp string.
temp = temp + "AY"; //adds "AY" to the temp string
cout << temp << " "; //prints out the word followed by a space.
}
string userIn("I NEED THIS IN PIG LATIN");
istringstream iss(userIn);
do
{
string sub;
iss >> sub;
piglatin(sub);
} while (iss);
while (iss >> sub)
piglatin(sub);
我不认为这是完全正确的猪拉丁语(我似乎记得有些单词是以元音开头的,你必须把辅音组移到末尾,而不仅仅是第一个),但如果必要的话,我会让你去解决这个问题
就循环的工作方式而言:
IAY EEDNAY HISTAY NIAY IGPAY ATINLAY
这将一直持续到提取一个项目(本例中的单词)失败为止。Naive代码(我承认我犯了罪)将使用以下内容:
void piglatin(string str)
{
string temp = str; //copies the string passed to the function into temp.
temp = temp + str.at(0); //adds the first character of the word to the end of the word.
temp = temp.erase(0, 1); //deletes the first character from the temp string.
temp = temp + "AY"; //adds "AY" to the temp string
cout << temp << " "; //prints out the word followed by a space.
}
string userIn("I NEED THIS IN PIG LATIN");
istringstream iss(userIn);
do
{
string sub;
iss >> sub;
piglatin(sub);
} while (iss);
while (iss >> sub)
piglatin(sub);
但这并没有考虑到这样一个事实:即使提取失败,您也可能不在文件末尾,例如,如果您的短语末尾有空格,或者如果您正在扫描整数,而流中的下一个标记是非整数
另外,顺便说一句,实际上不需要在piglatin()
中单独执行所有这些操作,也不需要(显式)临时字符串。您可以用以下内容替换全部内容:
do {
string sub;
iss >> sub;
piglatin(sub);
} while (!iss.eof());
void piglatin(字符串str){
cout…由于第一个答案中提到的bug,您的代码最终调用piglatin()并传递一个空字符串
此时,str.At(0)会导致明显的未定义行为(在一个完全空的字符串中没有字符#0),这会引发您正在抱怨的运行时异常。…并且由于第一个答案中提到的错误,您的代码最终会调用piglatin(),传递一个空字符串
此时,str.At(0)会导致明显的未定义行为(在完全空的字符串中没有字符#0)C++,你能把你所抱怨的运行时异常抛出吗?<你可以把你所得到的全部输出都贴上吗?这里是我的程序中所包含的指令。如果C++中有一个处理程序,那么这个程序可以安全地继续。你能把你所得到的全部输出都张贴吗?这里是我在程序中所包含的指令。td::内存位置0x0036F300超出范围。如果有此异常的处理程序,程序可能会安全地继续。你比我快:)是的,最初的猪拉丁语问题有不同的规则,但我们的教授给了我们不同的规则,让我们更容易,所以他说,取第一个字母,放在末尾,然后ad到单词的结尾,对用户输入的句子中的每个单词都这样做。@Koolkirtz:好的,忽略最后一点。我会把它放在那里让未来的人看,但是,如果你的教授给你一个不同的定义,显然你应该使用它。istream::eof()
不应用作循环条件;而应测试提取是否失败。如前所述,在读取所有输入字后,您可能会得到一个额外的对piglatin
的伪调用。(这将导致抛出异常,因为它在(0)
)我建议将循环更改为(string sub;iss>>sub;){piglatin(sub);}
。您可以在输入字符串的末尾添加空格进行测试。您比我快:)是的,原来的猪拉丁语问题有不同的规则,但我们的教授给了我们不同的规则,让我们更容易,所以他说,把第一个字母放在末尾,然后在单词的末尾加上ay,然后对用户输入的句子中的每个单词都这样做。@Koolki