Hazy McGee
Hazy McGee

Reputation: 12102

simple C++ functions

Ok so im sure im doing something stupid :D

i have a function:

int wordFunc(string a){
    std::ifstream inp;
    inp.open(a, std::ios::in);
    if (inp.is_open()){
        std::string word;
        unsigned long wordCount = 0;
        while(!inp.eof()){
            inp >> word;
            while(word.length() > 0){
                wordCount++;
            }
            inp.close();
        }
        return wordCount;
    }
}

the string is a user input file.txt - its set to be C:\Dump\user.txt right now

when i call the code with:

int main(){
    string file;
    int words = 0;
    file = "C:\\Dump\\user.txt";

    int a = wordFunc(file, words);
    cout << "Words: " << a << endl;

    return 0;
}

The console just halts - I havnt coded anything in C++ in many a year so im definitely rusty - any help?

EDIT With the help of some kind sould I ended up going like this

unsigned long wordFunc(const std::string& a){
    std::ifstream inp(a);
    system("cls");
    unsigned long wordCount = 0;
    std::string word;
    while(inp >> word)
    {
        wordCount++;
    }
    return wordCount;
}

For the function - should have posted an update

Upvotes: 2

Views: 304

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490178

C++ already has functions in the standard library to do most of what you're after, so you can just do something like:

#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>

int main() { 
    std::ifstream in("user.txt");

    std::cout << "Words: " 
              << std::distance(std::istream_iterator<std::string>(in),
                               std::istream_iterator<std::string>());
    return 0;
}

Gotta love code that needs five headers for three executable statements!

If you do insist on doing the counting yourself, I'd do something like:

std::ifstream in("user.txt");

std::string word;

while (in >> word)
    ++wordCount;

std::cout << "Words: " << wordCount;

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881643

You have quite a few problems there.


As another poster commented, the line:

while (word.length() > 0)

will loop forever, and you need to change it to:

if (word.length() > 0)

You mix integers and unsigned longs inappropriately. The wordCount and a variables, and the return value from wordFunc() should be the same.


You have the inp.close() inside the reading loop rather than outside where it belongs. This means it will close the file after processing the first line.


You also have the return statement inside the if statement meaning that you have a syntax error, not returning anything in one execution path (the one where the file couldn't be opened).

You need to swap the return and the closing brace that follows it.

This also means that the wordCount variable will have to be declared outside of the if statement as well (at the top level of the function).


I think fstream::open() takes a char* rather than a string so you should recode it as:

inp.open (a.c_str(), std::ios::in);

Your call to wordFunc(), with two parameters, doesn't match the prototype, which only has one.


There's some extraneous variables, such as words in main().


The last word is counted twice because the EOF flag is only set once you try to read beyond the end of the file. You can fix that with a simple modification to the if statement that increments wordCount:

if ((!inp.eof()) && (word.length() > 0)) {

With all those changes made, you end up with:

#include <iostream>
#include <fstream>

unsigned long wordFunc (std::string str) {
    unsigned long wordCount = 0;
    std::ifstream inp;

    inp.open(str.c_str(), std::ios::in);
    if (inp.is_open()) {
        std::string word;
        while (!inp.eof()) {
            inp >> word;
            if ((!inp.eof()) && (word.length() > 0)) {
                wordCount++;
            }
        }
        inp.close();
    }
    return wordCount;
}

int main() {
    std::string file;
    file = "user.txt";
    unsigned long a = wordFunc(file);
    std::cout << "Words: " << a << std::endl;
    return 0;
}

Upvotes: 1

Michael Anderson
Michael Anderson

Reputation: 73490

Your problem is this:

        while(word.length() > 0){
            wordCount++;
        }

This loops forever. You probably mean

        if(word.length() > 0){
            wordCount++;
        }

Upvotes: 4

Related Questions