using file data

I'm trying to add file data together

for example

a file has
1.. 2
3.. 4
5.. 6
7.. 8
9.. 10

and I'm trying to add the first column(1+3+5+7+9=25)

how would i write the program
Last edited on
Open stream
Initialize sum

While can read number
do add number to sum and discard the end of the line

Show sum
i dont understand what you mean by
While can read number
do add number to sum and discard the end of the line????
the first two i understand.
Which of these you are familiar with?

* while-loop
* adding to variable
* read an integer from stream
* test whether previous read has failed (read will fail when at end of file)
* http://www.cplusplus.com/reference/istream/istream/ignore/
i was just introduced to files and i don't know much so the only thing i know is opening files and reading them i got no clue how to add the value together

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
  #include<fstream>
  #include<cstdlib>
  #include<string>
  
  using namespace std;

  int main()
  {
	

	//Now begin reading values from the file.
	ifstream inFile;

	string filename;
	
	cout << "Which file would you like to open? Include the file's name and extension: "
		 << endl;
	cin >> filename;
	
	inFile.open(filename.c_str()); //attempt to open file for input
	
	if(!inFile.fail()) //if it doesn't fail, the file exists 
	{
		cout << "A file by the name " << filename << " exists."
			 << endl;
		
	}
	
	int value; 
	
	inFile >> value; //stores the first number in variable value
	
	while (inFile.good()) //continue until the end of the file
	{
	
		cout << value << " ";
		inFile >> value; //stores next number in variable value
	}
	
	inFile.close(); //closes the file
	
	return 0;
}
	
	

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 

Topic archived. No new replies allowed.