My teacher has given me an assignment to parse a text file that contains no more than 25 "Students" the Student object has a Name, and five grades contained within an array.

Now, I've set up the Student file, the Array template file per his instructions and i'm reading in his code as follows. (I've overloaded the << and >> operators as well)

f.open (FileName1, ios_base::in);
	if (f.is_open ())
			cout << FileName1 << "is open." << endl;
		else
			{
			cout << FileName1 << " was NOT opened." << endl;
			f.clear (); //What specifically does this clear?
		}
		f >> A[0];
		cout << A[0];
		f.close();
}

Here's a sample of what I'm taking in.

George
75,85,95,100,44

Now I know I forgot to implement something, what exactly I forgot I can't remember! (Smooth; right?)

At the moment what it's doing is taking the first line (The students name) and putting it in the correct place.

It's not taking the integer correctly unless I separate the numbers with an end of line, and I don't expect it to work any other way,but how do I change that default behavior?


Other useless rant/info:
At the moment I'm getting the correct numbers in the integers (I set up a watch and it's right), if I change the text file to make it so that it's one line after each other, they do work, so my overload operators seem to be working fine - I just need to change them to "End" at end of line OR coma's; how would I do that?

Is there a way to see how it works... normally; like the code they used to implement >> and << or... cout and cin? (Or actually, what do I actually need to overload here, what has the default behavior of stopping at end of line, I'm a bit confused)

Recommended Answers

All 5 Replies

You are reading file in line-by-line. As a result, your name is read correctly (as a string), but the numbers are read as a string as well ("75,85,95,100,44"). What you need to do is to parse the string and break it into numbers. That's all you need to do. If the format is always this way, you could expect that number string will come right after the name string line.

How would I go about doing that? How does cin work? I'm trying to avoid using C type Strings; reading the number as a String an then cutting it up and changing them into an int seems overly complicated/not efficient, is there a better way?

If they are numbers, read them as an integer.

I am, how do I stop reading them in at the coma though? Here's my read in method; (which I call in my overloaded operator<<)

ifstream& Student::ReadStudent(ifstream& in){
	in >> Name;
	for(int i = 0; i < 5; i++)
		in >> Grades[i];
	return (in);
}

Read the whole string line in first. Then read the string by each character. While going through each character, check for ','. You can do something like below.

/*
// not an efficient algorithm,
// but show how to obtain your score from string
str <- a string of scores
scorePos <- 0
multiplier <- 1
for i=0; i< str length; i++
  ch <- character of str at i
  if ch is not equal to ','
    grade[scorePos] += (convert ch to integer)*multiplier
    multiplier = multiplier*10
  else
    increment scorePos by 1
    reset multiplier to 1
*/
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.