[Solved] Issue with substr C++


As mentioned you can’t call substr() with a std::ifstream. What you probably meant was to take the parts from the line string read in

        a[i]= stoi(line.substr(4,2));
        name[i]= line.substr(18,15);
        b[i]= stoi(line.substr(36,1));

Also note you’ll need to convert the substrings extracted to a number (instead of assigning them directly), hence I have added the stoi().


As also mentioned in the comments on your question, you should do better error checks, and initialize all of your variables properly.
Your while() loop can simply be rewritten as

while (getline (myfile,line)) {
    // ...
}

The surrounding if() block

if (myfile.is_open()) {
    // ...
}

can be omitted.

0

solved Issue with substr C++