Ok so im sure im doing something stupid 😀
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
Your problem is this:
This loops forever. You probably mean