文件阅读程序有...黑屏问题

时间:2013-11-22 23:51:31

标签: c++ readfile

好的,所以我对此有点麻烦。这是作业,特别是我的C ++编程我最终,但我有我认为是一个逻辑缺陷。此时应该执行的所有操作都是从指定文件中读取信息,将其存储在一对并行的二维数组中(一个用于字符串数据,另一个用于数字数据),然后将其打印到屏幕上。还没有其他任何内容,直到项目的后半部分才需要。

当我运行代码时,它会加载控制台窗口,然后将其放在空白处。如果有人能够如此友好地指引我到这个问题的附近,我将不胜感激。

#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("filename");

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


    for (int rareReader = 0; rareReader < 80; rareReader++)
    {
        // edited for brevity. this section reads each individual component
        // of the file and inserts them into the appropriate array section.

        do
        {
            getline(inputFile, trash);
        }
        while (trash != "end-entry marker");
        // throws away the rest of each entry as trash
    }
    //felt it relevant to mention that the following close command is before all
    //lines of the file have been read and thrown out. not sure if relevant.
    inputFile.close();
}



void printData(int numericData[80] [4], string textData[80] [7])
{
    cout << setw(10) << "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";
    cout << setw(10) << "Poach" << endl;
        cout << setw(20) << "Drop";
    cout << setw(20) << "Steals";
    cout << setw(20) << "Location";



    for (int printCount = 0; printCount < 80; printCount++)
    {
        cout << setw(10) << 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];
        cout << setw(10) << textData[printCount][5];
        cout << setw(10) << textData[printCount][4] << endl;
        cout << setw(20) << textData[printCount][2];
        cout << setw(20) << textData[printCount][3];
        cout << setw(20) << textData[printCount][6];
        /* columns: 0 = name 1 = Weakness 2 = drops
        3 = stealables 4 = poach item
        5 = appearnce type 6 = location */
        //copy of note for convenience in reading
    }
}

1 个答案:

答案 0 :(得分:1)

该程序很可能悬挂在某个地方。尝试在程序开始时打印到屏幕。如果可行,则继续添加'cout'调用以打印更多调试消息,直到找到程序挂起的点。这是一种您将一次又一次使用的调试方法。有时也称为“洞穴人”调试,因为更高级的替代方法是附加调试器并逐步执行代码。