Problems with file I/O

Write a simple C++ program called sum.cpp. The program will:
1. Read a file that contains several integers
2. Print these integers to the screen.
3. Add these integers together to sum.
4. Print the sum on the screen.

I have some code already This is what I have:
#include <iostream>
#include <fstream>
#include <string>

int main() {
using namespace std;
ifstream inf("sum.ccp");
while (inf) {
std:: string strInput;
inf >> strInput;
cout << strInput << endl;
}
}


Please use code tags in future and explain specifically what your problem is.
Here is an easier way to do what you are trying. fstream can read directly to an int so you don't need to try and use strings in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

int main() 
{
	std::ifstream File{"File.txt");
	
	int Number = 0;
	int Sum = 0;
	
	while(File >> Number)
	{
		// Will loop over each number, handle Sum/printing etc here.  
	}
	
	return 0;
}
Thanks a lot I really appreciate it.
Topic archived. No new replies allowed.