Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 访问向量元素时的分段错误_C++ - Fatal编程技术网

C++ 访问向量元素时的分段错误

C++ 访问向量元素时的分段错误,c++,C++,这是我的节目,一个星球大战问答游戏 当我运行该程序时,它到达cast[5]部分,然后崩溃(分段错误) #包括 #包括 #包括 #包括 使用名称空间std; 类字符 { 私人: 先串; 最后一串; int插曲; 公众: 字符(字符串f、字符串l、整数e); 字符串getF(); 字符串getL(); int getE(); }; Character::Character(字符串f、字符串l、整数e) :第一集(f),最后一集(l),第五集(e) { } 无效重播(矢量和castFunc); int

这是我的节目,一个星球大战问答游戏 当我运行该程序时,它到达cast[5]部分,然后崩溃(分段错误)

#包括
#包括
#包括
#包括
使用名称空间std;
类字符
{
私人:
先串;
最后一串;
int插曲;
公众:
字符(字符串f、字符串l、整数e);
字符串getF();
字符串getL();
int getE();
};
Character::Character(字符串f、字符串l、整数e)
:第一集(f),最后一集(l),第五集(e)
{
}
无效重播(矢量和castFunc);
int main()
{
智力猜测;
智力得分=0;
向量投射;
因为“赫特人贾巴6”不是由两个字符串和一个数字组成的

因此:


myFile>>插曲获取一个字符串,而不是
int插曲;

在调试器中运行此操作!单步执行代码,观察变量。将输入文件格式化为代码;这样,就不清楚行与行之间是否有空白行,是否有名称。
赫特人贾巴
这是一组名称,哪个是
第一个,哪个是
一个是
last
?让我放大@n.m.的评论。当遇到这一行时,'Jabba'是你的名字,'the'是你的第二个名字,你试着读
Hutt
,因为
int
var
插曲
失败了,流现在处于失败模式,因此不好,while循环中断,没有更多的数据插入到后面最后一段数据(jabba)是不完整的,在
插曲
中包含不确定的内容,驻留在元素
[4]
中。孩子们,这就是为什么我们检查IO操作而不认为它们成功的原因。@WhozCraig还值得记住,以防万一,我们通常认为关于名称的事情是错误的。
#include<iostream>
#include<vector>
#include<string>
#include<fstream>

using namespace std;

class Character
{
  private:
    string first;
    string last;
    int episode;
  public:
    Character(string f, string l, int e);
    string getF();
    string getL();
    int getE();
};

  Character::Character(string f, string l, int e)
  :first(f),last(l),episode(e)
{
}

void readCast(vector<Character> &castFunc);


int main()
{
  int guess;
  int score = 0;
  vector<Character> cast;  
  cout<<"Welcome to the star wars quiz! I will tell you a character and you have to tell me what episode they first appeared in. Lets play!"<<endl;
  readCast(cast);  
  cout<<"What episode did "<<cast[0].getF()<<" "<<cast[0].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[0].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"What episode did "<<cast[1].getF()<<" "<<cast[1].getL()<<" first appear?"<<endl;
  cin>>guess; 
  if(guess == cast[1].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"What episode did "<<cast[2].getF()<<" "<<cast[2].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[2].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"What episode did "<<cast[3].getF()<<" "<<cast[3].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[3].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"What episode did "<<cast[4].getF()<<" "<<cast[4].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[4].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
 cout<<"What episode did "<<cast[5].getF()<<" "<<cast[5].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[5].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"What episode did "<<cast[6].getF()<<" "<<cast[6].getL()<<" first appear?"<<endl;
  cin>>guess;
  if(guess == cast[6].getE())
  {
    cout<<"Congratz! That is correct!"<<endl;
    score++;
  }
  else
  {
    cout<<"Sorry, that was not correct."<<endl;
  }
  cout<<"Thanks for playing the Star Wars Quiz!"<<endl;
  cout<<"Your score was "<<score<<" out of 6!"<<endl;

  return 0;
}

void readCast(vector<Character> &castFunc)
{
  string first;
  string last;
  int episode;

  ifstream myFile;
  myFile.open("star_wars.txt");
  while(myFile.good())
  {
    myFile>>first;
    myFile>>last;
    myFile>>episode;

    Character list(first,last,episode);
    castFunc.push_back(list);
  }
}

string Character::getF()
{
  return first;
}

string Character::getL()
{
  return last;
}

int Character::getE()
{
  return episode;
}
while(myFile.good())
   {
     myFile>>first;
     myFile>>last;
     myFile>>episode;

     Character list(first,last,episode);
     castFunc.push_back(list);
   }