Problems with overloading the extraction operator(">>")

I am writing a file that should be able to take in numbers from a file, save them in an object, and then write sums out to a file. I am attempting to over load the fistream, but I seem to have run into a loop. Even after my while loop that uses the fistream is finished the program recalls the last input command in the loop and so the loop will not break. Also I need the loop to stop when it reaches the end of the input file. For now I have just put in values, but how can I set the loop to break if it reaches the end of the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	

     #include <fstream>
        
     using namespace std;
     int main(){
        
        ifstream dataFile;
        ofstream outputFile;
        dataFile.open("data.txt");
        outputFile.open("output.txt")

        myobject INPUT1;
        myobject INPUT2;
        myobject bigIntSum;

        int control;
	while(control < 3){
	dataFile >> INPUT1;
	dataFile >> INPUT2;
	bigIntSum = INPUT1 + INPUT2;
	control++;
	} 
        dataFile.close();
        outputFile.close();
     }
       


My overload function looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
istream& operator >>(istream& in, bigInt& inObject) {

	char incoming[100];
	for (int search = 0; search < 100; search++) {

		in >> incoming[search];
		if (incoming[search] == 59) {
			break;
		}
		inObject.savedInt[search] = int(incoming[search]) - int('0');
		inObject.savedSize = (search+1);
	}
	return in;
}
Last edited on
I'm not sure I understand exactly what you are asking, but I think you will want to add a condition on line 7 of the operator>>() that will also break if the "in" file is no longer valid (eof, error, etc).
Topic archived. No new replies allowed.