-1
は、ここで私のプログラム、スターウォーズのクイズゲームです 私がプログラムを実行すると、それはキャストになる[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ファイル
アクバー提督6
ランド・カルリジアン5
パドメ・アミダラ1
ボバ・フェット5
ジャバ・ザ・ハット6
オビ=ワン・ケノービ4
Kyloレン7
これをデバッガで実行してください。コードをステップ実行し、変数を監視します。 – abelenky
入力ファイルをコードとして書式設定します。このように、名前の間に空白行があるかどうかは不明です。 –
'Jabba the Hutt'という名前は、最初のものと最後のものの束です。 –