For some reason, when I include the first for loop, the second for loop doesn't run and 0 keeps getting printed out. When I get rid of it, the program runs as expected (e.g., "aaaaa iiiii ooooo" gives me 15).
Version that works:
#include <iostream>
#include <string>
using namespace std;
int numOfVowels;
string phrase;
int main(){
getline(cin, phrase);
for (int j = 0; j < phrase.size(); j++)
{
if(phrase[j]=='a' || phrase[j]=='e' || phrase[j]=='i' || phrase[j]=='o' || phrase[j]=='u')
numOfVowels++;
}
cout << numOfVowels;
}
Version that doesn't work:
#include <iostream>
#include <string>
using namespace std;
int main() {
int numOfVowels, loop;
string phrase;
cin >> loop;
for(int i = 0; i < loop; i++) //removing this for loop makes the program work
{
getline(cin, phrase);
for (int j = 0; j < phrase.size(); j++)
{
if(phrase[j]=='a' || phrase[j]=='e' || phrase[j]=='i' || phrase[j]=='o' || phrase[j]=='u'|| phrase[j]=='y')
numOfVowels++;
}
cout << numOfVowels;
}
}
Can someone tell me what I'm doing wrong here? Thank you