这是我的节目,星球大战问答游戏 当我运行程序时,它会进入cast [5]部分,然后崩溃(分段错误)
#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;
}
这是star_wars.txt文件
海军上将Ackbar 6
Lando Calrissian 5
PadméAmidala1
Boba Fett 5
Hutt 6 Jabba
Obi-Wan Kenobi 4
Kylo Ren 7
答案 0 :(得分:1)
因为“哈特贾巴6”不是由2个字符串和数字组成的......
这样:
while(myFile.good())
{
myFile>>first;
myFile>>last;
myFile>>episode;
Character list(first,last,episode);
castFunc.push_back(list);
}
myFile&gt;&gt; episode获取字符串,而不是int episode;