我的程序崩溃了,老实说我很困惑(c ++)

时间:2014-08-25 18:55:48

标签: c++ crash

由于某些原因,我的程序在收到用户输入后立即崩溃。然而,最奇怪的部分是它仅在用户输入Yes时崩溃,而不是在输入n o时崩溃。我的想法是它与班级Players有关。我已经尝试过所有传统方法的调试。断点似乎不起作用,因为如果我把它放在cout << "Is this the first time...之后,由于它们尚未被分配,所以没有任何变量会有任何数据。在完成readPlayerData功能之前,它完美无缺地工作,然后崩溃开始了。

using namespace std;

int main() {
    mt19937 rand_engine(time(nullptr));
    vector<Player> plyrs;
    srand(time(NULL));
    bool firstTimer = false;
    cout << "Is this the first time this program has run?" << endl;
    string firstTime;
    cin >> firstTime;

    if (firstTime == "Yes" || firstTime == "yes") {
        firstTimer == true;

        for (int i = 0; i < 30; i++) {
            int limit = 18; // rand() % (25 - 18) + 18;
            for (int w = 0; w < limit; w++) {
                int z = plyrs.size();
                plyrs.resize(z + 1);
                plyrs[z].setPlayeName(rand_engine);
                plyrs[z].setPlyrVal();
                plyrs[z].setPlayerTeam(i);
            }
        }
        string human_input;
        cout << "Pick a # between 1-30, this will be your team: " << endl;
        cin >> human_input;
        int human_teamInput = 0;
        human_teamInput = atoi(human_input.c_str());
        ofstream plyrdata;
        plyrdata.open("plyr.txt");
        plyrdata << human_teamInput << endl;
        plyrdata.close();
        ofstream playerSaveData;

        playerSaveData.open("savedata.txt");

        for (int i = 0; i < plyrs.size(); i++) {
            playerSaveData << plyrs[i].plName << endl;
            playerSaveData << plyrs[i].plOvr << endl;
            playerSaveData << plyrs[i].ploaw << endl;
            playerSaveData << plyrs[i].pldaw << endl;
            playerSaveData << plyrs[i].pldspg << endl;
            playerSaveData << plyrs[i].pldsps << endl;
            playerSaveData << plyrs[i].plSpg << endl;
            playerSaveData << plyrs[i].plSps << endl;
            playerSaveData << plyrs[i].plteam
                           << (i < (plyrs.size() - 1) ? "\n" : "");
        }
        playerSaveData.close();
        assemble_Team_Data();
    }

    else {
        assemble_Team_Data();
    }
    Sleep(10000);
    return 0;
}

以下是readPlayerData()

的代码
void readPlayerData() {
    numFnames = 1;
    numLnames = 1;
    string line;
    ifstream fnameFile("PlayersFname.txt");
    if (fnameFile.is_open()) {
        while (fnameFile.eof() == false) {
            plFname.resize(numFnames);
            getline(fnameFile, line);
            numFnames += 1;
        }
        fnameFile.close();
    }
    ifstream lnameFile("PlayersLname.txt");
    if (lnameFile.is_open()) {
        while (lnameFile.eof() == false) {
            plLname.resize(numLnames);
            getline(lnameFile, line);
            numLnames += 1;
        }
        lnameFile.close();
    }
}
void setPlayeName(mt19937& rand_engine) {
    readPlayerData();
    uniform_int_distribution<int> fname_rand(0, numFnames);

    string fname = plFname[fname_rand(rand_engine)];
    uniform_int_distribution<int> lname_rand(0, numLnames);

    string lname = plLname[lname_rand(rand_engine)];
    plName = string(fname + " " + lname);
}

1 个答案:

答案 0 :(得分:2)

不太可能是你的崩溃,但这段代码非常糟糕:

if (fnameFile.is_open())
{
    while (fnameFile.eof() == false)
    {
        plFname.resize(numFnames);
        getline(fnameFile,line);
        numFnames += 1;
    }
    fnameFile.close();
}

它没有正确检查文件的结尾,因为文件结尾在 getline期间发生,而不是之前。并且line变量无处可去。尝试

while (getline(fnameFile,line)) plFnames.push_back(line);
fnameFile.close();