Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C++
 #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;
}


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
Posted
Updated 17-Mar-16 3:26am
Comments
jeron1 17-Mar-16 15:41pm    
Though it won't make much of a difference here, you should make it a habit of 'delete'ing everything that you created with 'new' after you are done with it.

The while-loops inside the if-statement are not doing what you want:
C++
while(getline(filename,str));

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.
 
Share this answer
 
Comments
Member 12398690 17-Mar-16 7:07am    
hank you i did but i just work but it takes only half from the file and output like

Shellharbour vs MTSO Warriors
vs
vs
50 vs 21

till half of the file
any suggestion ??
i want output like

<pre>Shellharbour vs MTSO Warriors
50 vs 21

and read all lines
Member 12398690 17-Mar-16 7:18am    
ok got it . thank you
Arthur V. Ratz 21-Mar-16 3:13am    
Good solution. +5
nv3 21-Mar-16 3:49am    
Thank you!
C++
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900