1

The following code compiles fine so there's no syntax errors. I am having trouble printing out the CSV file I read into this bit of code. I am not sure what is going on as this is a standard procedure, but would appreciate some input as to how to resolve this. I'd like to be able to print out the content of s on the console. Can you see why s is not printing on the console? Am I missing something?

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include<iterator>

struct field_reader: std::ctype<char> {

    field_reader(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask>
            rc(table_size, std::ctype_base::mask());

        rc[';'] = std::ctype_base::space;
        return &rc[0];
    }
};


struct Stud{
    double VehicleID;
    double FinancialYear;
    double VehicleType;
    double Manufacturer;
    double ConditionScore;


    friend std::istream &operator>>(std::istream &is, Stud &s) {
        return is >> s.VehicleID >> s.FinancialYear >> s.VehicleType >>      s.Manufacturer >> s.ConditionScore;
    }

    // we'll also add an operator<< to support printing these out:
    friend std::ostream &operator<<(std::ostream &os, Stud const &s) {
        return os << s.VehicleID  << "\t"
                  << s.FinancialYear << "\t"
                  << s.VehicleType    << "\t"
                  << s.Manufacturer   << "\t"
                  << s.ConditionScore;
    }
};

int main(){
// Open the file:
std::ifstream in("VehicleData_cs2v_1.csv");

// Use the ctype facet we defined above to classify `;` as white-space:
in.imbue(std::locale(std::locale(), new field_reader));

// read all the data into the vector:
std::vector<Stud> studs{(std::istream_iterator<Stud>(in)),
 std::istream_iterator<Stud>()};

// show what we read:
for (auto s : studs)
    std::cout << s << "\n";
}
user12205
  • 2,684
  • 1
  • 20
  • 40
  • ... and what's wrong? I think I've seen this code somewhere here today. – user35443 Jun 07 '15 at 16:06
  • I'd like to be able to print out the content of s on the console, I didn't think it'd be that complicated to be honest. Can you see why s is not printing on the console? Am I missing something? – theblokeasking Jun 07 '15 at 16:18
  • Compiles fine, but does it work? Print out the state of `in` and the size of studs to see if you managed to read anything. – user4581301 Jun 07 '15 at 16:22
  • Do you get any error? Anyway, we don't have your input file, so we can't say. Place here an example of your input. – user35443 Jun 07 '15 at 16:22
  • Change the declaration of the vector to: `static std::vector rc(classic_table(), classic_table() + table_size);` – David G Jun 07 '15 at 16:58
  • 1
    Thanks user above me, your answer along with answer below by @user35443 worked perfectly, I will edit my question to show solution thanks guys – theblokeasking Jun 07 '15 at 18:12
  • Don't edit the question to show the solution. Showing the solution is what the answers are for. If you want, you can add an answer of your own. – user12205 Jun 07 '15 at 18:29

1 Answers1

0
friend std::istream &operator>>(std::istream &is, Stud &s) {
    return is >> s.VehicleID >> s.FinancialYear >> s.VehicleType 
              >> s.Manufacturer >> s.ConditionScore;
}

Ignores the fact that the commas are present completely. The most primitive solution I could think of was

friend std::istream &operator>>(std::istream &is, Stud &s) {
    char comma;
    return is >> s.VehicleID >> comma
              >> s.FinancialYear >> comma
              >> s.VehicleType >> comma
              >> s.Manufacturer >> comma
              >> s.ConditionScore;
}
user35443
  • 6,309
  • 12
  • 52
  • 75
  • Hi. I don't know what I am doing wrong but I just tried to copy paste that and see what printed out on the console but nothing. I'd like to be able to print out the content of s, I didn't think it'd be that complicated to be honest. Can you see why s is not printing on the console? Am I missing something? – theblokeasking Jun 07 '15 at 16:17
  • 1
    Hello, thanks everyone that replied, the answer was a mix of this answer along with the answer by @0x499602D2 – theblokeasking Jun 07 '15 at 18:11