Cod sursa(job #793994)

Utilizator pkiulianPeca Iulian pkiulian Data 4 octombrie 2012 22:10:01
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#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. 
 */