Pagini recente » Cod sursa (job #1123401) | Cod sursa (job #2459399) | Cod sursa (job #137338) | Cod sursa (job #730305) | Cod sursa (job #793994)
Cod sursa(job #793994)
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
int x, y, suma=0;
string line;
ifstream inFile("adunare.in", ios::in);
int v[2];
int k = 0;
while (!inFile.eof()){
getline(inFile, line);
if (line!=""){
v[k] = atoi(line.c_str());
k++;
}
}
inFile.close();
ofstream outFile("adunare.out", ios::out);
for (int i=0;i<k;i++){
suma+=v[i];
}
outFile << suma;
outFile.close();
return 0;
}
/* NOTE
the getline function takes also the empty lines by default in the while loop.
Therefore I need to check whether or not the line is empty.
atoi converts a string to int.
*/