#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
usingnamespace std;
//returns the sum of letters there are in the words of a file
long count_letters(ifstream& in_stream);
int main()
{
ifstream in_stream;
in_stream.open("test3.txt");
long letters;
letters = count_letters(in_stream);
in_stream.close();
cout << "The number of letters in the file is: " << letters << endl << endl;
cout << "End of the program.";
char a;
cin >> a;
return 0;
}
/*
long count_letters(ifstream& in_stream)
{
bool more = true;
char next;
long i(0);
while(true)
{
in_stream.get(next);
cout << "hello";
if (cin.fail())
{
return i;
}
if((!isspace(next)) && (next != '.') && (next != ','))
{
i++;
}
return 1;
}
return 1;
}*/
long count_letters(ifstream& in_stream)
{
char next;
long i(0);
in_stream.get(next);
//cout << next;
while(!in_stream.fail())
{
if((!isspace(next)) && (next != '.') && (next != ','))
{
i++;
}
in_stream.get(next);
}
return i;
}
Don't mind the stuff that was "commented out." I don't know why but I think my count_letters function isn't working. it seems like it never enters the while loop. The first value that gets stored in the char variable "next" is some weird character that looks like "ll" only the second "l" has a hole in the middle of it. Someone told me it was a vertical bar character (http://en.wikipedia.org/wiki/Vertical_bar) and it seems like it doesn't matter what's in the input file (test3.txt in the above code), the function NEVER enters the while loop.
BTW, for the purpose of this program, a letter is anything that isn't a whitespace, a comma, or a period. thank you for reading this far.
Sorry. Can't help much. It worked fine for me.
To debug, check is_open() and fail() before calling count_letters, check that there is no typo in file name.
I know the location under which it's saved isn't the problem because it's in the same location where I've been saving all my other experimental text files which I use to test C++ programs, all of which have given me no problems so far.
I noticed the character saved in next is always a vertical bar. I have no idea why this is and I'm guessing it's the root of my problems but I have no idea why next would be set to a vertical bar.