i got no clue how to add the value together |
a+b?
[codex = y + z; // assign the result of y+z into x
x = x + z; // special case. Replace value of x with x+z
x += z; // compound addition. Same as x = x + z;
Your code treats the first read differently. That is slighly unintuitive.
Remember, that istream::operator>> returns reference to the istream.
(That does allow chain of operations, eg. cin >> a >> b >> c; )
Therefore,
1 2 3 4
|
inFile >> value;
if ( inFile.good() )
// is same as
if ( (inFile >> value).good() )
|
Furthermore,
1 2 3
|
if ( ! inFile.fail() )
// is same as
if ( inFile )
|
It is true that !fail() isn't exactly same as good(), but it is sufficient here.
Thus, your lines 32-39 can condense to:
1 2 3 4
|
while ( inFile >> value )
{
cout << value << " ";
}
|
That, however, reads all integers (unless the file contains non-integers too) from the file and prints them to one line. Printing intermediate values is a handy debugging method, but you don't really want to see the values. You want to calculate.
The loop again:
1 2 3 4 5 6
|
while ( inFile >> value ) // read (first) value from line
{
// add the value to the sum
// call inFile.ignore (with appropriate parameters) to skip the rest of the line
}
// print the sum
|