Hi, for my homework assignment my teacher wants us to read a file word by word and count the vowels and consonants in that word, among other manipulations of the word.
Everything my my program works except the counting the vowels and consonants part. This is my function for counting the vowels
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int countvowels(string word, double wordlength) // Counting the vowels
{
int vowelcounter; // number of vowels in the word
for(int i = 0; i<word.length(); i++)
{
if(word[i] == 'a' || word[i] == 'A' || word[i] == 'e' || word[i] == 'E' ||\
word[i] == 'i' || word[i] == 'I' || word[i] == 'o' || word[i] == 'O' || word[i\
] == 'u' || word[i] == 'U')
{
vowelcounter++;
}
}
return vowelcounter;
}
It's a good habit if only because when you do get around to working with other libraries, there might be issues with conflicting names if you just include the namespaces. It may not be a problem in this one example.
Also, those namespace prefixes let you know for sure where everything is. If you're a good typist, then the (variable) benefits do outweigh the (slim) costs. :)
I was ingrained in C++ for my degree but my goto prof always stated that including the std namespace was what I wanted.
Since then I have mostly been coding in C# and I include everything I need to using a "using" directive. For these dinky ass programs I still don't understand why people are so opposed to using the std namespace.
Wow, can't beleive I didnt notice that. I initialized several variables like that at other places in my code, oopsies haha. No I havent been introduced to switch statements, I am just starting filestreams. A lot of this stuff went over my head, but Ill get there someday.
Thanks for the help, and quick replies, its greatly appreciated. I know where Ill be coming back for help in the future. :)
Not a big deal, but when I use std::strings with a constant value I like to ensure that they'll be constructed just the once (I guess this might be wasted effort with modern compilers?).
And remember that a char not being a vowel does not make it a consonant. It could be a space or punctutation. So you might need to use the C library isalpha() call, too.