私のプログラムがクラッシュする理由を理解できないようです。 "//名前のオプションを表示する"のwhileループを削除すると、プログラムはうまく動作します。コードはGCCでコンパイルされ、警告は表示されません。それは私のコンパイラでしょうか?それはfstreamと関係がありますか?ヘルプをいただければ幸いです。このループはどのようにプログラムをクラッシュさせますか?
ああ。あなたの疑問を抱いているこのプログラムがdata.txtを読み込み、適切なデータをplayer関数のインスタンスにロードします。現時点では不完全な状態です。あなたのデバッガを使用して
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define cls system("cls");
bool Pload = false;
void menu();
struct player {
int Px, Py, life = 20;
string name = "";
};
main() {
menu();
}
void menu() {
string cLine,names,input;
int x,i,lineNum = 0;
fstream data;
menu:
data.open("data.txt");
//Gets list of all names in data.txt, Adds them to string names
while(data.good()) {
getline(data,cLine);
if(cLine[0] == '/') {
names += cLine;
}
}
names += '\n';
//Displays name options
cls
cout << "Welcome to W A L K.\n\nWhat is your name?\n";
while(names[i] != '\n')
{
cout << i;
if(names[i] == '/') {cout << endl;i++;} else {cout << names[i];i++;}
}
cout << endl;
getline(cin,input);
//checks if name exits and loads file data into player/world objects
data.close();
data.open("data.txt");
while(data.good()) {
lineNum++;
getline(data,cLine);
if(cLine.erase(0,1) == input) {
cls cout << "Found Name" << endl;
getline(cin, input);
}
}
//Restarts menu
data.close();
goto menu;
}
data.txtを
/Sammy
x:0
y:0
l:20
\
/Mary
x:7
y:9
l:20
\
/Dill
x:7
y:9
l:20
\
/Jack
x:7
y:9
l:20
\
本当に助けてくれました。私は投票をするだろうが、これは私の最初の質問の一つです。ありがとうございました。 –