0

I am attempting to create a program to take in file input the user specifies, then count the frequency of each letter, then produces individual text files for each paragraph that it counts.

This program compiles perfectly on Microsoft Visual Studio Enterprise 2017. However, when I attempt to run it on my schools computer (running GCC 4.8.5), the program fails and throws errors such as:

"no matching function for call to ‘std::basic_ifstreamr>::open(std::string&, const openmode&)’"

Any thoughts or suggestions?

letterCount.hpp

#ifndef LETTERCOUNT_HPP
#define LETTERCOUNT_HPP

#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::ofstream;

class letterCount

{
private:
public:

    //Letter counting and output functions
    void countTheLetters(ifstream &, int*);
    void outputTheLetters(ofstream &, int*);
};
#endif

letterCount.cpp

#include "letterCount.hpp"
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ios;
using std::ofstream;
using std::ifstream;

//Function for count_letters

void letterCount::countTheLetters(ifstream &finput, int* count)

{
    //Declaring a string variable
    string stringOfWords = "", line;
    int k = 0;
    char character;
    //While loop through the text
    while (getline(finput, line))
    {
       if (line.empty()) {
          break;
       }
       else {
          //Lines are concatinated
          stringOfWords += line + ' ';
       }
    }
    //White space removal
    stringOfWords.erase(stringOfWords.length() - 1);
    //Value is zero
    for (k = 0; k<26; k++) {
       count[k] = 0;
    }
    //Count each letter individually
    for (k = 0; k<stringOfWords.length(); k++) {
       //One letter at a time
       character = tolower(stringOfWords[k]);
       //Character validation
       if ((int)character >= 97 && (int)character <= 122){
          //Update the frequency counter
          count[(int)character - 97] += 1;

       }

    }

}

//Function for output_letters
void letterCount::outputTheLetters(ofstream &foutput, int* count){
    //Variable placeholder declaration
    int k;
      //For loop to print out the frequency of the letters
    for (k = 0; k<26; k++){
       //Output to file
       foutput << (char)(k + 97) << " - " << count[k] << "\n ";

    }

}

main.cpp

//Standard include files, including letterCount.hpp
#include "letterCount.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ios;
using std::ofstream;
using std::ifstream;

int main() {

    ifstream finput;
    ofstream foutput;
    string input, output;

    int count[26] = { 0 }, k = 1;

    //Request user input of the file to be used
    cout << "\n Enter the input file - ";
    cin >> input;

    //Open the file
    finput.open(input, ios::in);

    //While loop through the file
    while (finput.good()) {

       //Countint the letters
       letterCount a;
       a.countTheLetters(finput, count);
       cout << "\n Enter the output file " << k << " - ";
       cin >> output;

       //The number of paragraph is updated
       k = k + 1;

       //Open the receiving file
       foutput.open(output, ios::out);
       a.outputTheLetters(foutput, count);

       //Close the file
       foutput.close();

    }

    //Close the input file
    finput.close();
    system("pause");
    return 0;

}

makefile

output: main.o letterCount.o
    g++ -std=c++0x main.o letterCount.o -o output

main.o: main.cpp
    g++ -c main.cpp

letterCount: letterCount.cpp letterCount.hpp
    g++ -c letterCount.cpp

clean:
    -rm *.o output
IP Address
  • 15
  • 2
  • 6

1 Answers1

0

I cannot reproduce this with my G++ 7.2.0 compiler, but I think it's possible that your compiler didn't find the cast from std::string to const char*, which is the type of the first parameter of std::basic_fstream::open(). So using c_str() you can yield one for use:

finput.open(input.c_str(), ios::in);
foutput.open(output.c_str(), ios::out);
iBug
  • 35,554
  • 7
  • 89
  • 134