Hi,
I have a csv file that looks like:
Anna
1, 2, 3, 4
Bert
4, 5, 6, 9.5
Iga
5, 7, 7, 10
I have the following Header:
The Output is:
Anna;;
Bert;;
Iga;;
Problem is after read the csv file, the values of notes are not displayed. Why? I suppsoe there is something wrong with the ostream function. So any suggestions to correct my code?
I have a csv file that looks like:
Anna
1, 2, 3, 4
Bert
4, 5, 6, 9.5
Iga
5, 7, 7, 10
I have the following Header:
##include <fstream>
##include <sstream>
##include <vector>
##include <iterator>
#include <alorithm>
class Class_Child
{
private:
std::string name;
std::vector<double> note;
public:
bool operator <(Class_Child const&other) const {
return name< other.name;
}
// Insertion operator: write data out
friend std::ostream &operator<<(std::ostream &os, Class_Child const &child)
{
os << child.name<<'\n';
os << child.note.size();
for (int i=0; i<child.note.size; ++i)
os << child.note[i];
return(os);
}
// Extraction operator: Read data in
friend std::istream &operator>>(std::istream &stream, Class_Child &child)
{
// read the entire line into a string
std::getline(stream, child.name);
//Read the line of scores and convert them into double
std:: string scores;
std::getline(stream, scores);
std::stringstream scorestream(scores);
std::copy (std::istream_iterator<double>(scorestream),
std::istream_iterator<double>(),
std::back_inserter(child.note));
return stream;
}
};
Main
int main()
string Filename;
cout << "Give name of input file: "; // Child.csv
cin >> Filename;
std::ifstream dataFile(Filename.c_str());
// Declare a vestor of children
std::vector<Class_Child> children;
if (! dataFile)
{
cout << "Error opening input file" << endl;
return -1;
}
// Read characters in from file
//Copy all children into vector
std::copy(std::istream_iterator<Class_Child>(dataFile),
std::istream_iterator<Class_Child>(),
std::back_inserter(children));
dataFile.close();
std::copy(children.begin(), children.end(),std::ostream_iterator<Class_Child>(std::cout,"\n"));
return 0;
}
The Output is:
Anna;;
Bert;;
Iga;;
Problem is after read the csv file, the values of notes are not displayed. Why? I suppsoe there is something wrong with the ostream function. So any suggestions to correct my code?