![](/img/trans.png)
[英]c++ terminate called after throwing an instance 'std::out_of_range' what(): basic_string::substr: __pos > this->size()
[英]terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
从本质上来说,我是c ++的完整入门者,直到昨天我才完成Bucky的C ++教程。 该程序应该删除日文/中文字符,然后用户输入与字符串相对应的文本。 我没有遇到任何编译错误,但是,每当我尝试运行此错误时,我都会得到标题中的错误。 当我运行此程序时,抛出“ std :: out_of_range” what()的实例后,我得到错误终止,调用what():basic_string :: substr:__pos(为1)> this-> size()(为0)。 我已经查询了其他有此问题的人stackoverflow.com/questions/27222447/stdout-of-range-what-basic-stringsubstr-pos和其他人,但是我不理解所提供的答案,因为它直接针对他们的代码和我很新。 码:
#include <iostream>
#include "Values.h"
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void Wrong();
void start();
void Correct();
string top;
string book;
string fish;
string umbrella;
string dictionary;
string sea;
string person;
string green;
string morning;
string school;
string water;
string dog;
string cat;
string weather;
string homework;
string bookstore;
string CurrentWord;
int main(){
ofstream nihon("Words.txt");
string top = "Pronounciation: Meaning: Hiragana: Kanji:";
string book("\nHon Book ほん 本"); //1
string fish("\nSakana Fish さかな 魚"); //2
string umbrella("\nKasa Umbrella かさ 傘"); //3
string dictionary("\nJisho Dictionary じしょ 辞書");
string sea("\nUmi Sea うみ 海");
string person("\nHito Person ひと 人");
string green("\nMidori Green みどり 緑");
string morning("\nAsa Morning あさ 朝");
string school("\nDaigaku University だいがく 大学");
string water("\nMizu Water みず 水");
string dog("\nInu Dog いぬ 犬");
string cat("\nNeko Cat ねこ 猫");
string weather("\nTenki Weather てんき 天気"); //13
string homework("\nShukudai Homework しゅくだい 宿題");
string bookstore("\nHonya Bookstore ほんや 本屋");
nihon << top << book << fish << umbrella << dictionary << sea << person << green << morning << school << water << dog << cat << weather << homework << bookstore;
// Nihon = Japan, Closing Nihon? :)
nihon.close();
string CurrentWord;
/* try
{
throw 1;
}
catch (std::exception const &exc)
{
std::cerr << "Exception caught " << exc.what() << "\n";
}
catch (...)
{
std::cerr << "Unknown exception caught\n";
}*/
start();
return 0;
}
void Wrong(){
cout << "You're answer is not correct :(" <<endl;
start();
}
void start(){
srand(time(0));
int pRandomnumber = 1+(rand()%15);
ifstream nihongolearn("Words.txt");
switch(pRandomnumber){
case 1:
CurrentWord = book;
break;
case 2:
CurrentWord = fish;
break;
case 3:
CurrentWord = umbrella;
break;
case 4:
CurrentWord = dictionary;
break;
case 5:
CurrentWord = sea;
break;
case 6:
CurrentWord = person;
break;
case 7:
CurrentWord = green;
break;
case 8:
CurrentWord = morning;
break;
case 9:
CurrentWord = school;
break;
case 10:
CurrentWord = water;
break;
case 11:
CurrentWord = dog;
break;
case 12:
CurrentWord = cat;
break;
case 13:
CurrentWord = weather;
break;
case 14:
CurrentWord = homework;
break;
case 15:
CurrentWord = bookstore;
}
string answer;
string huff = CurrentWord.substr(30, 49);
cout << "What does "<< huff << " Mean?" << endl;
cin >> answer;
string displayed = CurrentWord.substr(19, 30);
if(answer!=displayed){
Wrong();
}else{
Correct();
}
}
void Correct(){
cout << "You're answer is correct :D" << endl;
start();
}
您实质上是在尝试从空字符串中提取子字符串。
发生这种情况是因为您的主要功能未在全局范围内初始化变量。
相反,它构造局部变量,该局部变量在主函数之外不可见。
调用start
,它访问全局变量(仍保持不变,因此为空)并崩溃。
我建议您进一步了解C ++变量作用域规则,以便完全理解您的错误。
一个简单的解决方案是停止在主函数中为字符串构造阴影变量,而仅分配现有的全局变量。
首先,您要全局声明字符串top
(在顶部),然后在main()
中声明一个名为相同top
的局部变量。 有内容的是本地的。 我建议您将字符串分配移动到最顶部
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void Wrong();
void start();
void Correct();
string top = "Pronounciation: Meaning: Hiragana: Kanji:";
string book = "\nHon Book ほん 本";
string fish = "\nSakana Fish さかな 魚";
string umbrella = "\nKasa Umbrella かさ 傘";
string dictionary = "\nJisho Dictionary じしょ 辞書";
string sea = "\nUmi Sea うみ 海";
string person = "\nHito Person ひと 人";
string green = "\nMidori Green みどり 緑";
string morning = "\nAsa Morning あさ 朝";
string school = "\nDaigaku University だいがく 大学";
string water = "\nMizu Water みず 水";
string dog = "\nInu Dog いぬ 犬";
string cat = "\nNeko Cat ねこ 猫";
string weather = "\nTenki Weather てんき 天気";
string homework = "\nShukudai Homework しゅくだい 宿題";
string bookstore = "\nHonya Bookstore ほんや 本屋";
string CurrentWord;
另外,在使用substr()时,请确保字符串具有足够的字符以避免此错误。 在您的代码中,您使用了两次
string huff = CurrentWord.substr(30, 49);
string displayed = CurrentWord.substr(19, 30);
第一个从位置30开始,从那里开始需要49个字符,这意味着您将需要(字符串中30 + 49个字符)以防止出现任何错误。
CurrentWord
将包含在您的代码中的字符串, CurrentWord
42-43个字符。 因此,显然超出了范围。
string top = "Pronounciation: Meaning: Hiragana: Kanji:";
string book( "\nHon Book ほん 本"); //1
string fish( "\nSakana Fish さかな 魚"); //2
string umbrella( "\nKasa Umbrella かさ 傘"); //3
string dictionary( "\nJisho Dictionary じしょ 辞書");
string sea( "\nUmi Sea うみ 海");
string person( "\nHito Person ひと 人");
string green( "\nMidori Green みどり 緑");
string morning( "\nAsa Morning あさ 朝");
string school( "\nDaigaku University だいがく 大学");
string water( "\nMizu Water みず 水");
string dog( "\nInu Dog いぬ 犬");
string cat( "\nNeko Cat ねこ 猫");
string weather( "\nTenki Weather てんき 天気"); //13
string homework( "\nShukudai Homework しゅくだい 宿題");
string bookstore( "\nHonya Bookstore ほんや 本屋");
我建议更改第二个参数,只要求输入7个字符左右,这样它就不会用完字符串。 还要考虑\\ n取决于系统,它可以算作一个或两个字符。
string huff = CurrentWord.substr(30, 7);
string displayed = CurrentWord.substr(19, 7);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.