samedi 27 juillet 2019

Why am I getting a conversion function error?

I have to read in an external file called text.txt. The file could contain a lot of data (and def > 83 chars) and I want to ignore the spaces. Then I need to get the total amount of chars in the file (not incl. spaces). I have to use strlen. I am getting a conversion type error which is confusing because I thought strlen returns an int. The error is where I have made the text bold.

int main() {

fstream inFile;
string inputString;

inFile.open("text.txt", ios::in);

if (inFile) {
    getline(inFile, inputString, ' ');
    while (inFile)
    {
        int tempStringLength = strlen(**inputString**);
        int total = 0;
        total += tempStringLength;
    }
}
else
    cerr << "Unable to open file text.txt";
    exit(1);

    return 0;

}

I expect the total to be the total # of chars in the file.

Aucun commentaire:

Enregistrer un commentaire