jeudi 9 mars 2017

C++ ifstream error using string as opening file path

I get this error when I try running the program.What might be the problem as the code is correct as far as I can see.

Here is the error

std::basic_fstream::basic_fstream(std::string&, const openmode&)'

Here is the code

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
string fileName;
int frequencyArray[26];
char character;

for (int i = 0; i < 26; i++)
    frequencyArray[i] = 0;

cout << "Please enter the name of file: ";
getline(cin, fileName);

fstream inFile(fileName, fstream::in);  // to read the file

if (inFile.is_open())
{
    while (inFile >> noskipws >> character)
    {
        // if alphabet
        if (isalpha(character))
        {
            frequencyArray[(int)toupper(character) - 65]++;
        }
    }

    inFile.close();

    cout << "Letter frequencies are as: " << endl;
    for (int i = 0; i < 26; i++)
    {
        cout << (char)(i + 65) << " = " << frequencyArray[i] << endl;
    }
}
else
{
    cout << "Invalid File. Exiting...";
}


return 0;
}

Aucun commentaire:

Enregistrer un commentaire