How can i get the digits out from file and the player who scored the most points?

Input file to read from
Match A score: 3-1
Mark
Peter
Peter
Match B score: 1-1
Jim

underlined - player who scored the points

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("inputFile.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      // how to get the variable and players name?      
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Recommended Answers

All 19 Replies

Don't use the return value of eof() to control the input loop. Use this instead:

while (getline (myfile, line))

>>How can i get the digits out from file

You have to parse the input, either during the read or after. Looks like the file is a line with a score, the first numerical value being the number of names to follow. The first numerical value follows a colon, so you can use getline to read up to and remove the colon, then use the operator to get the numerical value, then ignore the rest of the line.

To determine who scored the most points you could create a container of user defined type that contains a string for a name and an int to keep tabs of the number times the name is read. When the file read is complete sort the user defined types based on the numerical field to find the largest or assume the first has the most points and if you find someone with more as you loop through the rest of the container keep track of it instead.

i don't quite understand what you meant Can you show me some examples?

Possible parsing algorithm:

getline(fin, line); //read in line
istringstream iss(line);//use an istringstream to parse line
getline(iss, debri, ':');//ignore up to and including the colon
iss >> num;//save the first int
getline(iss, debri);//ignore the rest
for(int q = 0; q < num; ++q)//print out the indicated number of names
{
  fin >> name;
  cout << name << endl
}

thanks..

iss >> num;//save the first int
getline(iss, debri);//ignore the rest

wouldnt num be 3-1 ??


sample file input
Match A score: 3-1
Mark
Peter
Peter
Match B score: 1-1
Jim
Match C score 1-5
Mark

How can i make it such that it can print out
- points conceded
- games won/drawn/lost

If it works like I think it should then >> will stop input into the int when it encounters the dash.

You could probably separate the 3-1 by doing:
iss >> num1;
iss >> dashChar;
iss >> num2;

getline(iss, debri, ':');

may i know what's debri

debri is an STL string that I ignore in the code snippet posted. As Salem pointed out so eloquently, I won't win any spelling bees.

i can't seem to be able to compile properly

> i can't seem to be able to compile properly
I just woke up, and there's snow outside.

Wanna continue trading useless info, or are you going to post some code and some error messages.

You've changed the code since the first post, but we've got no idea what sort of a mess you've made of it, based on what's been posted so far.

This is the code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {

  string line, name;
  int num;    
  ifstream myfile ("inputFile.txt");
  
  if (myfile.is_open())
  {
    while (getline (myfile, line))
    {
		getline(myfile, line); 
		istringstream iss(line);
		getline(iss, debris, ':');
		iss >> num;//save the first int
		getline(iss, debris);//ignore the rest
		for(int q = 0; q < num; ++q)
		{
		  myfile >> name;
		  cout << name << endl;
		}
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

You're using istringstream without including <sstream>
Also 'debris' is undeclared.

You're reading 5 or 6 times from your file per loop, are you sure you want to do that....

How do i declare debris?I only need to read the file once and store the variables.

how about : string debris; ...

oh, it's not an object? my apologies..

You're reading 5 or 6 times from your file per loop, are you sure you want to do that....

How can i make it to read the file once?

while (getline (myfile, line))
{
    // one line read and stored in "line"
    // now do stuff with it
}

I suspect niek_e didn't read the entire thread before writing post #13. In this case multiple file reads per loop is appropriate. That's because with each loop through the file you want to extract all the information regarding 1 event. Each event is an initial line with information that needs to be extracted followed by a variable number of lines containing individual strings. The number of strings to read after the initial line is part of the information to be extracted from the initial line.

Even more important for Jason123, however, is that most of the information provided here can't be just cut and pasted into your code. It will need some modification. Often times the information is an example of solving the problem as it comes up in a different context that you will need to adjust to your code. Sometimes it is a discussion regarding the problem. Sometimes you need to add things like header files, variables, etc. So please don't take the lazy way out and simply try cut and paste without thinking about what you are doing.

I suspect niek_e didn't read the entire thread before writing post #13. In this case multiple file reads per loop is appropriate.

You're right, I didn't: if(posts_in_thread>10) laziness+=100; Sorry about that, I was wrong. :S

Do regard me as a beginner as i had just started C++ about 3 week ago and this is my 1st programming language. Of course, most of the stuff i have yet to gain knowledge on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.