Because your method of getting the next character is by lab3.get(character)
, incrementing the character in your word count check is not going to get the next character, only alter the value of the character you have.
Rather than trying to 'look ahead', consider preserving the last character read and checking against it to detect the end of the word on the following iteration.
if (ispunct(character)) { countpunc++; if (isaalpha(prevchar)) { words++; }
} if (isspace(character)) { countspace++; if (isaalpha(prevchar)) { words++; }
}
with prevchar
initialized to zero before the loop starts, and then set equal to character
inside the loop after all of your checks.
Also note that your numbers check is really capturing digits, so a value of 10 in your data will count as 2 numbers in your output rather than one.
manpreet
Best Answer
2 years ago
I'm trying to learn some coding to broaden my scope of knowledge, and I've seemed to run into a bit of a conundrum.
I'm trying to create a program to output the number of characters, digits, punctuation, spaces, words and lines that are being read in from a file.
Here is the text file I am reading in.
Here is my code