嘿伙计们我正在编写一个程序,从文件中读取数据并将其存储在结构中,我有几个问题。文本文件如下所示:
Azrin, Neil 2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6 Babbage, Charles 2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
该行中的第一个数字是难度级别,接下来的7个数字是分数,接着是24个不同的名字。
我的程序将打印所有潜水员名称,然后是难度级别。然后它只会打印潜水员#1的第一个分数,而不是所有分数。
另外,对于潜水员#2的分数,它打印出第二个分数(第3个在线数字)而不是第一个分数(第2个在线数字)。等每个潜水员。
我该怎么做才能阅读并打印每位潜水员的所有分数?
我一直在努力工作几个小时,所以非常感谢任何帮助!
我的代码:
for (lcv = 0; lcv < NUM_NAMES; lcv++) //reads in all of the data
{
getline(inFile, diveList[lcv].diversName);
inFile >> diveList[lcv].diff;
for (count = 0; count < NUM_SCORES; count++)
{
inFile >> diveList[lcv].scores[count]; //the problem may be here on down below where i print out the data... im not sure
}
inFile.ignore(200, '\n');
}
printTableOne (diveList);
void printTableOne (const diverList diveList)
{
int lcv;
int count = 0;
cout << "The number of divers for round 1 is: " << NUM_NAMES << endl << endl;
for (lcv = 0; lcv < NUM_NAMES; lcv++)
{
cout << "Diver #" << lcv << endl;
cout << setprecision(1) << fixed << endl;
cout << "NAME: " << diveList[lcv].diversName << endl;
cout << "DIFFICULTY LEVEL: " << diveList[lcv].diff << endl;
cout << "SCORES: " << diveList[lcv].scores[lcv] << endl; //problem here??
cout << endl << endl;
}
答案 0 :(得分:0)
问题确实存在于
行cout << "SCORES: " << diveList[lcv].scores[lcv] << endl;
这里你只打印一个分数,它使用与潜水员相同的索引。因为你有更多的潜水员而不是分数,很快你就会从在阵列中打印分数。
这里需要一个循环,就像你从文件中读取分数一样。