gpt4 book ai didi

c++ - 文件读取程序有...黑屏问题

转载 作者:行者123 更新时间:2023-11-28 07:16:29 25 4
gpt4 key购买 nike

好的,所以我在这方面遇到了一些麻烦。这是家庭作业,特别是我最后的 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
}
}

最佳答案

程序很可能卡在某处。尝试在程序开始时打印到屏幕。如果可行,则继续添加“cout”调用以打印更多调试消息,直到找到程序挂起的位置。这是一种您将反复使用的调试方法。有时称为“穴居人”调试,因为更高级的替代方法是附加调试器并单步执行代码。

关于c++ - 文件读取程序有...黑屏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20156614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com