The code is simple, and its not an assignment. She says theres nothing wrong with it, and i totally disagree. It outputs a second copy of the last digit entered, and i think it lies in the first while loop. Example, say i enter
1
2
3
-999
According to theory this places 1, 2, and 3 into the file lined as shown above, and -999 quits that while loop. This is where the problem is. Now if i remeber there is a marker of some sort added to the end of a file to mark the end, could this extra 3 be that marker? Or is the loop screwey? She says its supposed to do that, that she made it that way. I say the code is errorous but im not sure why. I've never used ifstream and ofstream this way before, so i really dont know.
My beef with this is shes teaching the other students to this code saying its right and i say its errorous, and it ........es me off that she cant explain to me how her design calls for this. She said, and i quote "it doesnt matter how u alter it, its c++ doing it".
Bull$$$$....please look at this and help me out, i cant prove her wrong alone lol. Also note the way we have been doing it is to NOT have a pre-created file when its run, just let it do it. The file also cannot be specified to .txt/.rtf and so on, no extensions.
Code:#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; const int SENTINEL = -999; int main(int argc, char* argv[]) { int data; ofstream outfile; ifstream infile; outfile.open("c:\myfile"); cout << "Enter an integer or -999 to quit: "; cin >> data; while (data != SENTINEL) { outfile << data << endl; cout << "Enter an integer or -999 to quit: "; cin >> data; } outfile.close(); infile.open("c:\myfile"); while (! infile.fail()) { infile >> data; cout << data; } return 0; }



LinkBack URL
About LinkBacks



) it won't work.