Hi everyone. I got a few projects from school today. The last one I need to do is make a program were the user enters a vowel from the keyboard. If it is a vowel display "Ok, if not... dispaly "Please enter a vowel. I have the basis of the program down and it compiles and works fine. I am having problems figuring out how to store the Vowels though. At first I thought an Enum would work being
enum Vowel { A, a E, e, I, i, O, o ,U ,u};
but I get one of two errors being that "A is undefined and the other being that there are two many enum points.
I changed the enum to just declaring chars but that dosen't work either nor does an array.
Does anyone have any idea as to how I can store the vowels so I can refrence them in the "for" statements?
#include <string>
#include <iostream>
usingnamespace std;
int vowel;
char vowels = A, a, E, e, I, i, O, o, U, u;
int main()
{
cout << "Please enter a vowel: " << endl;
cin >> vowel;
if(vowel == vowels)
{
cout << "Ok" << endl;
}
if(vowel != vowels)
{
cout << "Please enter a vowel" << endl;
}
return 0;
}
You probably need to start out by making the vowels variable character array. Then you could loop through the array and compare each element to the input.
for (int i = 0; i < /*TODO: Replace with the vowel count*/; i++)
{
vowels[i]; //Accesses the ith element of vowels.
//TODO: Use the above snippet in some way.
}
for (int i = 0; i < /*TODO: Replace with the vowel count*/; i++)
{
*(vowels+i); //Accesses the ith element of vowels.
//TODO: Use the above snippet in some way.
}
Above is for those hardcore die-hard C fanatic crazy with everything that is pointer related even for array access.
Please use Albatross solution for easy maintenance of your code in future.
I'm sorry for all the noob questions ( it's been years since I've programmed). I looped the array and got it working. Everytime I enter a key though it says "Ok" even if it is not a vowel. Here is what I have so far.
Really quick I wanted to say I appericate the help. Kooth I treid what you recommended and now when I enter a vowel they all come back as OK, regaurdless of if it is a "Correct" vowel or not. I must be missing something here; here is another look at what I have so far...
In the "for" loop it should be if( vowel == vowels[ i ] ) (two '==' instead of one '='.)
Also, you do not need the (vowels+i); statement at all.
Also, you are going to want to take the else out of your "for" loop. When you get a match and output "OK", you need to do this:
1. Set a flag to indicate that you have a match.
2. Break out of your "for" loop.
3. At the end of your "for" loop, check the new flag and if it is not set, output "Please enter a vowel".
I changed the "If" statement and took out the "(vowels+i);" statement. For the love of me though I can't get this stupid thing to work :p Every time I try it displays "Ok" regaurdless of type. After I took the "else" loop out it started displaying "Please enter a vowel" everytime regaurdless. What's weird is I added a "break;" after the "if" loop. It should break the if loop when a match has been found and it isn't. Does anyone have any idea or could some how demonstate to me :/ I am going crazy over this!
#include <string>
#include <iostream>
usingnamespace std;
int vowel;
int i;
char vowels [] = { 'A', 'a', 'E', 'e', 'I', 'i' , 'O', 'o', 'U', 'u'}; // the compiler calculates the needed number of char for you
int main()
{
bool ok = false;
while(! ok)
{
cout << "Please enter a vowel: " << endl;
cin >> vowel;
// for (int i = 0; i < vowels[i]; i++)
for (int i = 0; i < (sizeof(vowels) / sizeof(vowels[0])); i++) // so does (sizeof(vowels) / sizeof(vowels[0]))
{
ok = (vowel == vowels[i]) // assignment (=) for vowel is wrong
if(ok)
{
cout << "Ok" << endl;
break;
}
}
}
return 0;
}