程序错误地读取文件

时间:2013-11-23 22:42:04

标签: c++ visual-studio-2012

好吧,我即将做一些我讨厌的事情,因为我的问题是要求我发布我的代码。

这是一项家庭作业。我需要为这个项目的一半做的只是读取文件中的数据,存储它,然后将数据打印出来。数据(在这里找到:www.gamefaqs.com/ps2/459841-final-fantasy-xii/faqs/42703只是因为我错过了一些关于数据的东西)只是一份来自最终幻想12的稀有产卵列表。有s一系列条目,我有一组循环来分开并凑成一对数组。

问题是,有些条目给我带来了打印不应该的数据或将数据放在错误位置的问题。几个条目,其中一系列星号表示怪物的难度,应该根据星号放入的临时变量的大小转换为数字格式,而不是读取一个不同的变量(不是全部他们,只是其中一些)。此外,条目51和65不会破坏他们应该做的一切,而是踢回我不想要打印的东西。

所有它应该做的是接收关于怪物的重要统计数据(名称,HP,XP,LP,它的弱点,什么时候它会消失,什么可以被盗,什么是从Poach尝试得到的,原始的精灵使用,它是多么艰难[1-5的规模],以及它产生的地方)。应该抛弃其他所有东西。在那之后,它应该打印我告诉它的统计数据,并按我指定的顺序打印。它适用于/大多数/他们。

我使用调试器已经完成了十几次代码,我仍然不确定它到底在哪里。即使只是简单地“看看循环的这一部分”也会有很大的帮助。

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

void readFile(int numericData[80] [4], string textData[80] [7]);
void printData(int numericData[80] [4], string textData[80] [7]);

int main()
{

    string trash;
    string textData [80] [7];
    /* columns: 0 = name 1 = Weakness 2 = drops
    3 = stealables 4 = poach item
    5 = appearnce type 6 = location */

    int numericData [80] [4];
    // columns: 0 = HP 1 = XP 2 = LP 3 = diff rank
    // used 2 multi-dimensional parallel arrays to avoid 10 parallel arrays

    readFile(numericData, textData);
    printData(numericData, textData);

    return 0;
}

void readFile(int numericData[80] [4], string textData[80] [7])
{
    ifstream inputFile;
    inputFile.open("80RaresFFXII.txt");

    string trash;
    string rankConverter;
    for (int i = 0; i < 169; i++)
    {
        getline(inputFile, trash);
    }
    //throws out first 169 trash lines, until first usale line.


    for (int rareReader = 0; rareReader < 80; rareReader++)
    {
        getline(inputFile, trash);
        getline(inputFile, trash);
        getline(inputFile, trash);
        // first three lines of EACH entry are trash: empty line,
        // rare number, and empty line

        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        // reads the portion 'Name : ' and trashes

        getline(inputFile, textData[rareReader][0], '(');
        // stores name of monster

        getline(inputFile, trash);
        // throws away the rest of the Name line
        /* NOTE: this somehow fixed a different problem with
        entry 52 and on some how gumming up the works 
        look at cosmetic fixes when they crop up*/

        getline(inputFile, trash);
        // trashes emplty line after name

        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        inputFile >> numericData[rareReader][0];
        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        inputFile >> numericData[rareReader][1];
        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        inputFile >> numericData[rareReader][2];
        // stores HP, XP, and LP values

        getline(inputFile, trash);
        // trashes emplty line after LP

        getline(inputFile, trash, ':');
        if(trash == "\nWeak  ")
        {
        getline(inputFile, trash, ' ');
        getline(inputFile, textData[rareReader][1]);
        getline(inputFile, trash, ':');
        }
        else
        {
            textData[rareReader][1] = "None";
        }
        // one entry has missing line here

        if (trash == "Drops ")
        {
        getline(inputFile, trash, ' ');
        getline(inputFile, textData[rareReader][2]);
        getline(inputFile, trash, ':');
        }
        else
            textData[rareReader][2]= "N/A";

        if(trash == "Steal ")
        {
        getline(inputFile, trash, ' ');
        getline(inputFile, textData[rareReader][3]);
        getline(inputFile, trash, ':');
        }
        else
            textData[rareReader][3] = "N/A";

        if (trash == "Poach :")
        {
            getline(inputFile, trash, ' ');
            getline(inputFile, textData[rareReader][4]);
            getline(inputFile, trash, ':');
        }
        else
        {
            textData[rareReader][4] = "N/A";
        }
        // note: poach exists on only some entries.
        // if/else statement exsists to compensate for variation

        getline(inputFile, trash, ' ');
        getline(inputFile, textData[rareReader][5]);
        /* stores weaknes, drop items, poachable,
         stealable items, and the base sprite 
         image used for the rare */

        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        getline(inputFile, rankConverter);

        numericData[rareReader][3] = rankConverter.size();
        // takes in number of stars in Rare Rank, then coverts
        //the size to numerical form.

        getline(inputFile, trash, ':');
        getline(inputFile, trash, ' ');
        getline(inputFile, textData[rareReader][6]);
        // stores Location of rare

        if(rareReader > 79)
        {
            do
            {
                getline(inputFile, trash);
            }
            while (trash != "-------------------------------------------------------------------------------");
            // throws away the rest of each entry as trash
        }



    }
    inputFile.close();
}



void printData(int numericData[80] [4], string textData[80] [7])
{
    cout << setw(20) << "Name";
    cout << setw(10) << "HP";
    cout << setw(10) << "XP";
    cout << setw(10) << "LP";
    cout << setw(6) << "Rank";
    cout << setw(10) << "Weakness";
    cout << setw(10) << "Sprite\n";
    /*cout << setw(10) << "Poach\n";
        cout << setw(20) << "Drop";
    cout << setw(20) << "Steals";
    cout << setw(20) << "Location \n\n";*/



    for (int printCount = 0; printCount < 80; printCount++)
    {
        cout << setw(20) << textData[printCount][0];
        cout << setw(10) << numericData[printCount][0];
        cout << setw(10) << numericData[printCount][1];
        cout << setw(10) << numericData[printCount][2];
        cout << setw(6) << numericData[printCount][3];
        cout << setw(10) << textData[printCount][1] << endl;
        cout << setw(10) << textData[printCount][5];
        cout << setw(10) << textData[printCount][4];
        cout << setw(20) << textData[printCount][2] << endl;
        cout << setw(20) << textData[printCount][3];
        cout << setw(20) << textData[printCount][6] << endl;


        /* columns: 0 = name 1 = Weakness 2 = drops
        3 = stealables 4 = poach item
        5 = appearnce type 6 = location */
        //copy of note for convenience in reading
    }
}

0 个答案:

没有答案