Reputation: 9
I'm trying to count the total number of vowels in a string. I'm using strlen to get the total length of the string but then when I try and count through the string by each letter it says C++ forbids comparison. So I assume something is wrong in my if statement.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char sentence[] = "";
int count;
int total;
int length;
int lengthcount;
int output;
output = 0;
length = 0;
count = 0;
total = 0;
cin >> total;
while (total != count){
cin >> sentence;
length = strlen(sentence);
while (length != lengthcount)
if (sentence[length] == "a" ||sentence[length] == "e"||sentence[length] == "i"||sentence[length] == "o"||sentence[length] == "u"||sentence[length] == "y"){
++ output;
++ lengthcount;
else{
++lengthcount;
}
}
++count;
}
return 0;
}
Upvotes: 0
Views: 305
Reputation: 52602
char sentence [] = "" produces an array sentence with a length of 1.
cin >> sentence isn't going to work very well, is it, if sentence cannot hold more than one character and one character is already needed for the trailing nul byte?
lengthcount is an unitialised variable, and the rest of the code just makes my head hurt.
Upvotes: 0
Reputation: 4409
See Nialls answer for one of the main problems.
The algorithmic problem with your code is again in the if
statement.
sentence[length]
returns the last character of your c_string (in this case, the null character '/0'
that terminates the string).
Your if statement should look more like:
if (sentence[lengthcount] == 'a'\
||sentence[lengthcount] == 'e'\
||sentence[lengthcount] == 'i'\
||sentence[lengthcount] == 'o'\
||sentence[lengthcount] == 'u'\
||sentence[lengthcount] == 'y')
{
\\do something
}
Please remember to pre-allocate space for the string too, i.e.
char sentence[50];
which would give you space for 49 chars + terminator.
Alternatively, use a std::string
Upvotes: 2
Reputation: 516
If you wish to count the total number of vowels in the given string, you need to use sentence[lengthcount]
. Lets say the sentence
is abc
strlen(sentence)
would return 3, and since in c++, the indexing begins with 0 and not 1, therefore sentence[length] would check for '\0' hence in the entire loop you check against the last value which is '\0' which is meaningless. Also, don't forget to initialize lengthcount. Rest all the things per-mentioned.
Upvotes: 1
Reputation: 30604
sentence[length]
is a single character. It should be compared to a 'a'
and not "a"
.
"a"
is a character array and direct comparison with the built in operator==
is not supported.
sentence[index] == 'a'; // where index is probably lengthcount in your example
Should do the trick. If use of std::string
is an option, you should favour that over char
arrays.
In addition, your char sentence[] = "";
will need some more space than just the '\0'
character. Some alternatives include the use of std::string
and std::getline
or char[nnn]
with cin.get(...)
to make sure that you don't overrun the buffer you allocate.
Upvotes: 4