Where did I go wrong in my code for word count C++

I edit your code, Do you mean something like this?

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

int wordCount(ifstream& in_stream, ofstream& out_stream);

int main()
{
    char inputFile[100];
    ifstream fin;
    ofstream fout;

    cout << "Enter a File name: " << endl;
    cin >> inputFile;

    fin.open(inputFile);
    if (fin.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    int WordCount = wordCount(fin, fout);
    fin.close();
    fout.close();


    return 0;
}

int wordCount(ifstream& in_stream, ofstream& out_stream)
{
    int counter = 0;
    char data[100];

    in_stream >> data;
    while (strlen(data)>0)
    {
        counter++;
        in_stream >> data;
    }
    return counter;
}

Leave a Comment