I'd expect a warning but not an error for unitialized variable. You can initialize present and next to anything you want as long as you change it to a meaningful value before you try to use it. Initialize it to a tilde or an asterix or something if you want so it is easier to see if the default initialized value persists beyond expected interval.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
I have trouble figuring out why you need both next and present in countWords, but that would seem to be a possible run time error as opposed to compile time error.
If you are obliquely asking for help in evaluating the error in main() then you will need to post the error and pertinent part of main() (or the entire thing if it isn't too long).
Edit:
Well fibber my gibbet. Looky there. My typing has really slowed.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
So if you were counting words in a theme paper you wouldn't want to count spaces as the only demarker of words because there are consecutive spaces at the end of each sentence? Only in your case there may be zero, one or more leading spaces in a line.
//don't count more than one space if they are consecutive
get(first char) //get first char
while not at end of file //get all char in file
if present char is space
increase word count
get(next char) //ignore any consecutive spaces
if next char is space
while not end of file and next is space
fin.get(next)
if end of file
break out of loop
else
present = next //assign first non space char after space char(s) to present
else
fin.get(present)
Edit: Drat, beat again.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Try this;
fin.get(present); //get first char in file
while(!fin.eof()) //read all char in file
{
if(present == ' ')
{
++wordCount;
fin.get(next);
//ignore any consecutive spaces
if(next == ' ')
{
while(!fin.eof() && next == ' ')
{
fin.get(next);
}
if(fin.eof)
break;
else
present = next;
}
else
present = next
}
else
fin.get(present);
}
post file to read
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
please post file you are trying to read.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Isn't the End-Of-Line also considered the end of a word? And it's not a SPACE.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
The final line may not have a new line char, it may have EOF, so that will need to be considered as well.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396