NOT WORKING | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

NOT WORKING

Hi guys , I have a little problem with cpp code The program has to count whe vowels and the consonants in the word , but its not working correctly here is the code: #include <iostream> using namespace std; int main() { char word[100]; cout << "Inserisci la parola: "; cin >> word; char* pWord = word; pWord[0]; int vowels = 0, consonants = 0; for (int i = 0; i < sizeof &pWord; i++) { switch (*pWord) { case 'a': vowels++; break; case 'e': vowels++; break; case 'i': vowels++; break; case 'o': vowels++; break; case 'u': vowels++; break; default: consonants++; break; } pWord++; } cout << "The word is: " << word << endl; cout << "The vowels are: " << vowels << endl; cout << "The consonants are: " << consonants << endl; }

2nd Feb 2021, 8:17 PM
Hammad Yaqub
Hammad Yaqub - avatar
1 Answer
+ 4
If you use 'sizeof' on pWord you will get the size of the pointer (not the number of elements in the array). If you use sizeof(word) you will get the number of elements in the entire array - not just the portion of it that makes up the string. There are two things you can do: 1.) Get the string length of pWord or word and compare it against 'i'. 2.) Or loop until pWord hits a NUL-terminator: '\0'. If you use the first method you don't have to count consonants. You can just subtract the vowel count from the string length.
2nd Feb 2021, 9:23 PM
Mike A
Mike A - avatar