Search code examples
c++parsinggetlinestringstream

C++ Exercise in Parsing with Classes, getline, and stringstream


I've been really struggling to figure out how to use getline and stringstream together to parse a file. If someone could help explain I'd be extremely grateful. EXTREMELY.

I'm really trying to build the function ReadFile, which is a function that reads in a filename comprised of "name, salary" format text for 50 names and salaries. I want to separate it into the arrays named: names[] salary[].

What I have so far is below, I think my class structure is alright according to the prompt, but could someone help me in reading the file into the separate arrays properly? Thank you!

class Players {

    public:
        //Initialize
        float salaries[50];
        string names[50];

        Players(){
            for (int i=0; i<50; i++) {
            float salaries[i]={-1.0};
            string names[i]= {""} ; }
        }

        void ReadFile(string filename) {
            ifstream file;
            file.open(filename);

            string line;
            int index=0;
            while (getline(file, line)) {
                stringstream ss(line);
                getline(ss, names[index], ',');
                getline(ss, salaries[index]);

                index ++
                string names;
                float salaries;

                ss >> names >> salaries >> ',';           
            }
        }

    float MaxSalary() {

    }

    string MaxSalaryName() {

    }
};

Solution

  • This code should not compile. In the future please ensure your code examples compile without modification, and include all your #includes.

    A few things about your ReadFile function:

    • You are redefining your variables names and salaries. You probably don't want that.
    • index ++ should be index++;.
    • ifstream::open takes a C string, so you need to pass in filename.c_str().
    • Your first getline is fine, but your second is attempting to copy a string directly into a float. You need to explicitly do the type conversion. One way is to create a temporary string variable, pass it to getline, then use std::stof to convert that string into a float. This requires C++11. If you can't use C++11, you could use strtof from <cstdilib>.
    • Not sure what the ss >> ... is meant to achieve, but you probably don't want it. If you want to print your results, you want something like cout << names[index] << ": " << salaries[index] << "\n";

    A few things about your Players() constructor:

    • You are redefining your member variables because you include types in front of the variable names. Remove the types.
    • The default string constructor creates an empty string, so there is no need to initialize it with an empty string.
    • You can safely drop the {} around the initialization values.

    Most of these problems could have been caught by looking at compiler errors and then looking up the appropriate C++ documentation. That said, I know C++ isn't the easiest language to get started in and it helps allot to have pointers early on. Keep at it and you will get it!