This is what I've come up with so far. My professor made it clear he wants us to use the string class to get familiar with it. I don't know what function to use to test the input file string for vowels. This example compiles and runs but prints out vowels = 4 which isn't correct. Any hints?
The problem is that str.find('a' || 'A') is basically just like saying str.find(true) -- it won't look for 'a'or'A'; it'll just evaluate ('a' != 0) or ('A' != 0) and pass that result to find.
I would use find_first_of: (warning: code below untested)
1 2 3 4 5 6
auto vowelPos = str.find_first_of("aeiouAEIOU");
while (vowelPos != std::string::npos)
{
++vowels;
vowelPos = str.find_first_of("aeiouAEIOU", vowelPos + 1); // Find next occurrence
}
Wow that worked! Thank you so much! I am fairly new to C++ so I am still getting used to the different functions. One thing though, in my code I used size_t instead of auto, what is auto?
In long double main's code snippet, vowelPos is technically size_t.
auto is a C++11 declaration specifier keyword, which allows the compiler to deduce what type a variable should be based on its initialization expression.
In other words, std::string.find_first_of() returns a size_t object, therefore vowelPos will be a size_t.
You shouldn't worry about auto, seeing as how you're in a beginner class, and you're probably not using a C++11 compiler.