how can i modify this code if i want tor use a file CSV? the cellules are separated by " ;" and the firs line contains the name of the column
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <stdexcept>
/** @file **/
#include <fstream>
#include <iostream>
using namespace std;
int main() {
int volume,volumet=0,month,i=0;
ifstream f("volume.txt");
if(f){
while(f){
f>>month>>volume;
i++;
volumet=volumet+volume;
}}
cout<<volumet/i;
f.close();
}
|
this is what i have tried to do but i haven't succed
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
|
#include <stdexcept>
/** @file **/
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
ifstream f("volumes.csv");
int volumet=0,i=0;
string entete;
getline(f, entete);
cout << "EntĂȘte: " << entete << endl;
string ligne;
string month;
string volume_str;
int volume;
while(f){
getline(f, month, ';');
getline(f, volume_str);
if ( volume_str.size() > 0 and volume_str[volume_str.length()-1] == '\r' )
volume_str.resize(volume_str.length() - 1);
istringstream(volume_str) >> volume;
i++;
volumet=volumet+volume;
}
cout<<volumet/i;
f.close();
}
|
Last edited on