问题描述
在这里,我尝试将团队名称和得分存储在阵列中,例如team1和team2,分别为2支球队,得分1和得分2,各队的得分一切正确,但没有存储在阵列中。当我打印文件时,只有Vs打印不是团队和得分。有人请帮帮我。谢谢
here i tried to store team names and score in array such as team1 and team2 for 2 teams and score1 and score2 for score of respective teams everything is right but it didn't store in array. when i print the file only "Vs" is printing not the team and score. somebody please help me for this. thanks
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string *team1,*team2,*score1,*score2,s;
int count=0;
string str;
fstream inputfile("results.txt");
if(!inputfile)
{
cout<<"No input File"<<endl;
}
while(getline(inputfile,s))
{
if(s.length()!=0)
{
++count;
}
}
count=count/2;
team1=new string[count];
team2=new string[count];
score1=new string[count];
score2=new string[count];
inputfile.close();
fstream filename("results.txt");
for(int i=0;i<count;)
{
if(i%2==0)
{
while(getline(filename,str));
istringstream stream(str);
getline(stream,team1[i],':');
getline(stream,team2[i],':');
//cout<<"team 1 = \t"<<team1<<" VS "<<"team 2 = \t"<<team2<<endl;
}
else if(i%2!=0)
{
while(getline(filename,str));
istringstream stream(str);
getline(stream,score1[i],':');
getline(stream,score2[i],':');
//cout<<"team1score = \t"<<score1<<" VS "<<"team2score = \t"<<score2<<endl;
}
i++;
}
for(int i=0;i<count;i++)
{
cout<<team1[i]<<" Vs "<<team2[i]<<endl;
cout<<score1[i]<<" Vs "<<score2[i]<<endl;
}
return 0;
}
输入文件是
谢尔哈伯:MTSO勇士队
50:21
马刺队:山猫队
23:28
猛龙队:小牛队/>
37:12
马刺队:MTSO勇士队
20:12
小牛队:山猫队
20:26
猛龙队:MTSO勇士队
16:26
猛龙队:谢尔哈伯
9:50
马刺队:小牛队
24:15
山猫队:MTSO勇士队
21:12
马刺队:MTSO勇士队
17:26
小牛队:谢尔哈伯
10:64
猛龙队:小牛队
32:14
山猫队:马刺队
2:20
小牛队:MTSO勇士队
8:48
谢尔哈伯:山猫队
44:6
猛龙队:马刺队
27:17
小牛队:山猫队
8:26
MTSO勇士队:猛龙队
16:26
马刺队:谢尔哈伯尔
14:39
小牛队:马刺队
20:53
猛龙队:谢尔哈伯尔
9:30
MTSO勇士队:山猫队
18:13
猛龙队:山猫队
16:13
谢尔哈伯:小牛队
58:5
MTSO勇士队:马刺队
11:18
小牛队:猛龙队队员
18:22
山猫队:马刺队/>
22:18
MTSO勇士:谢尔哈伯
5:61
马刺队:猛龙队
34:26
我的尝试:
i已经尝试过,直到我上面提交,请帮助我让它运行正常。提前谢谢
the input file is
Shellharbour:MTSO Warriors
50:21
Spurs:Bobcats
23:28
Raptors:Mavericks
37:12
Spurs:MTSO Warriors
20:12
Mavericks:Bobcats
20:26
Raptors:MTSO Warriors
16:26
Raptors:Shellharbour
9:50
Spurs:Mavericks
24:15
Bobcats:MTSO Warriors
21:12
Spurs:MTSO Warriors
17:26
Mavericks:Shellharbour
10:64
Raptors:Mavericks
32:14
Bobcats:Spurs
2:20
Mavericks:MTSO Warriors
8:48
Shellharbour:Bobcats
44:6
Raptors:Spurs
27:17
Mavericks:Bobcats
8:26
MTSO Warriors:Raptors
16:26
Spurs:Shellharbour
14:39
Mavericks:Spurs
20:53
Raptors:Shellharbour
9:30
MTSO Warriors:Bobcats
18:13
Raptors:Bobcats
16:13
Shellharbour:Mavericks
58:5
MTSO Warriors:Spurs
11:18
Mavericks:Raptors
18:22
Bobcats:Spurs
22:18
MTSO Warriors:Shellharbour
5:61
Spurs:Raptors
34:26
What I have tried:
i have tried till i submit above, please help me to make it run properly. thanks in advance
推荐答案
while(getline(filename,str));
这会将整个文件读取到EOF标记。你想要的只是阅读一行。所以只需删除while,然后离开getline调用即可。对于else分支也一样。
This will read the entire file to the EOF marker. What you intended was just to read a single line. So simply remove the while and just leave the getline call. Same for the else branch.
while(getline(filename,str));
除了读取到文件末尾之外,该语句末尾的分号确保您的循环没有任何用处。您可以通过使用向量或类似集合来大大简化代码,以便在读取数据时存储数据,而无需首先计算行数。
The semicolon at the end of that statement ensures that your loop does nothing useful, apart from reading to the end of the file. You could simplify your code considerably by using a vector, or similar collection, to store the data as you read it, without the need to count the lines in the first place.
这篇关于C ++代码从数组中的文件存储中读取并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!