I'm in desperate need for help. No one I know understands C++. I've searched the web for whatever useful info but ended up with finding tips for integers.
I would like to create a program that fulfills these design specifications:
Input: Any alphanumeric "word".
Output:
If there is an even number of vowels, the actual number will be displayed. (e.g. Number of vowels: 3)
If there is an odd number of vowels, the number of consonants and numbers will be displayed. (NOT vowels) (e.g. Number of Non-Vowels: 7)
I'm quite confused about how the compiler can check each letter in the input word.
I've tried equating the string that contains the input and the string vowel="aeiou" but I can't get the program correctly. Please help!
Thank you very much for checking my post! I'm very grateful! I'm just new to programming.
I used the for-statement loop to make all the inputs lowercase so when I input any case of exit it would exit the program. I've tried using the same technique with finding the vowels with a for loop but I ended up getting bugs.
With my online tutorial knowledge, I've done this.
This is my current code with many missing parts
I tried this but I'm not sure how to count the vowels and also the consonants.
I tried the for-loop so when the letter is not null it will continue to the next letter in the word. Whenever the letter in the word[i] is the same as any letter in the vowel string, the number will add 1 and same goes for the consonants.
It didn't work unfortunately.
// if it finds the character inside conso
if ((conso.find(word[i])>= 0) && (conso.find(word[i])< conso.length()))
{
number = number +1; // use a single = and not ==
}
// if it finds the character inside vowel
elseif ((vowel.find(word[i])>= 0) && (vowel.find(word[i])< vowel.length()))
{
numbera = numbera +1;
}
I'm not sure how to effectively use find but I wrote this code while figuring things out. Kindly help me with it.
The program does not run unfortunately. It tells me that there's something wrong in the if (word[i]="a") num = num + 1;
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word;
int num(0);
int main()
{
do
{
cout << "Enter an Alphanumeric word!" << endl;
cin >> word;
for (int i=0; word [i] != '\0'; i++)
{ word[i] = tolower (word[i]); }
for (int i=0; word [i] != '\0'; i++)
{ if (word[i]="a") num = num + 1;
elseif (word[i]="a") num = num + 1;
elseif (word[i]="e") num = num + 1;
elseif (word[i]="i") num = num + 1;
elseif (word[i]="o") num = num + 1;
elseif (word[i]="u") num = num + 1; }
cout << num << endl;
;
cout << num << endl;
} while (word != "exit");
system ("pause");
return 0;
}
I'll try doing the find since it would help me in future programming. I feel like a caveman with computers...
word[i]="a" is wrong because word[i] is a single character and "a" a string. Also a simple = sets the value.
if you wonna compare it, use word[i]=='a'
Checking each letter should work, but if you want to do the same with all letters of conso it will be much code.
Or there are only letters allowed as input (no numbers or special signs)?
for example if string[i] is an 'e', the first if-condition is true (false || true || false .. = true).
So it increases vowel by 1.
After this als the second if-condition is true, because 'e' is not an 'a', not an 'i' and so on
(true || false || true || true || true = true).
So it also counds 'e' as a nonVowel.
EDIT: In my opinion, whenever it is possible (I beleive it's always possible) use char[] arrays rather than strings.
I disagree strongly.
I'm not std::string's biggest fan (I actually dislike a lot of things about it), but it's a lot better/safer than char arrays for casual string manipulation.