hey what's up guys, I'm writing some C++ code used to do math operations on polynomials that are contained in a input file. Right now I'm stuck on the actual streaming in of the files. The .txt input file is in the format as follows
1 1
2 2
3 3
XXX
5 5
6 6
The XXX separates two different polynomials. The coefficient number comes first, and then the exponent. The problem that I believe I am having right now is that the eof bit is returned after the XXX is read in. The debug seems to indicate that the XXX is actually read in, but the stream then returns eof. I may be wrong, but that's what the problem seems most likely to be.

This is my code where the file is streamed in

in >> tempcoef >> tempexpo;
	while(in)
	{
		n.newterm(tempexpo, tempcoef);
		in >> tempcoef >> tempexpo;
		if (!in)
		{
			in.ignore(2);
			in >> tempcoef >> tempexpo;
			while(in)
			{
				n2.newterm(tempexpo, tempcoef);
				in >> tempcoef>> tempexpo;
			}
		}
	}

in is my input file, n and n2 are classes that contain linked lists, tempexpo and tempcoef are variables used to store the numbers, and newterm is a function i created to insert nodes in the linked lists

Edited 4 Years Ago by tj104: n/a

You are attempting to read XXX into your integer values.

Why are you reading in so many places? Think through your design again.

I thought about that, but I'm not sure how I can get past it. The input list won't be limited to just 3 values, it can be any arbitrary amount. Any way that I think about it, XXX has to be read in at some point.

The multiple sets of read ins are used to place the values into two different classes. I've been trying to think of a way of using one set of reads to put the values into two classes, but I haven't been able to think of anything.

You can always read data in as a string. If the input is numerical in value, then convert the sring to a numerical type. If not, then the only non numerical string in your file tells you to stop input into the current polynomial.

This article has been dead for over six months. Start a new discussion instead.