Greetings,
I have written this code which compiles and runs great BUT...
I am not coming up with the same answers in the book, can someone help to find out why, plz?
Here is my code
And my data for text file data.dat:Code:#include <iostream> //cin,cout,<<,>>
#include <fstream> //ifstream,ofstream
#include <string> //string, getline()
#include <cassert> //assert()
using namespace std;
int num1, num2, num3, num4;
int main()
{
cout << "This program computes num1 thru num4\n"
<< "and places its results in another file.\n\n";
// ------------------input section-------------------------------------------------
cout << "Enter the name of the input file: ";
string inputFileName;
getline(cin, inputFileName); //get name of input file
ifstream inStream; //open an input stream to the input file
inStream.open(inputFileName.data()); //establish a connection
assert(inStream.is_open()); //check for success
for(;;) //forever loop
{
inStream >> num1 >> num2 >> num3 >> num4; //read a value
if(inStream.eof()) break; //if eof, quit
}
inStream.close(); //close the connection
// ------------------------output section-----------------------------------------
while(getchar() != '\n');
cout << "Enter the name of the output file: ";
string outputFileName;
getline(cin, outputFileName);
ofstream outStream(outputFileName.data()); //open an output stream to the output file
// and establish a connection
assert(outStream.is_open()); //check for success
outStream << num1 << num2 << num3 << num4; //write a value
outStream.close(); //close stream
cout << "Processing complete.\n";
return 0;
}
1 -2 3
4 -5 6
7 -8 9
also my output file is labeled answer.dat.
The answers I get are the following:
967-8
BUT...
the book says:
num1= 1, num2= -2 , num3= 3, and num4= 4
Help me become unconfused to why my numbers don't match?
:confused:
next I need to change inStream like so and see its output but its always the same as before, why?
like so:
inStream >> num1 >> num2;
inStream >> num3;
inStream >> num4;