Storing Char Problems

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?

Here is what I have so far


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>
	using namespace 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;
}

Last edited on
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.
Last edited on
I tried to make the array, buy I am not 100% certain how to loop though them. Would you mind showing me?
1
2
3
4
5
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.
}


Does this help?

-Albatross
It does. Thank you so much!
1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
	using namespace std;

	
int vowel;
char vowels [10] = { 'A', 'a', 'E', 'e', 'I', 'i' , 'O', 'o', 'U', 'u'};

int main()
{
	int i;

	
	cout << "Please enter a vowel: " << endl;
	cin >> vowel;


	for (int i = 0; i < vowels[10]; i++)
	{
		 *(vowels+i); //Accesses the ith element of vowels.
	}

	if (vowel == )
	{
		cout << "Ok" << endl;
	}

	else
	{
		cout << "Please enter a vowel" << endl;
	}



	int keepWindowOpen;
	cin >> keepWindowOpen;


	return 0;
}



Can someone tell me what I am doing wrong?



EDIT: I noticed that the (vowel == ) in the "if" statement is missing a part. That was ment to be (vowel == vowels[10];)
Last edited on
A couple of things:

First, vowels[ 10 ] is not a valid index! Valid indices are 0 through 9.

Second, you should compare vowel to vowels[ i ] in your "for" loop and if they match, then output "OK".
Last edited on
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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
	using namespace std;

	
int vowel;
int i;
char vowels [10] = { 'A', 'a', 'E', 'e', 'I', 'i' , 'O', 'o', 'U', 'u'};

int main()
{


	
	cout << "Please enter a vowel: " << endl;
	cin >> vowel;


	for (int i = 0; i < vowels[i]; i++)
	{
		 *(vowels+i);   //Accesses the ith element of vowels.	   \

		if (vowel = vowels[i])
		{
			cout << "Ok" << endl;
		}

		else 
		{
			cout << "Please enter a vowel"	<< endl;
		}
	}




	return 0;
}

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!
Can you post your code?
hello strychnine213,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <iostream>
	using namespace 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;
}
Last edited on
OK, you are missing a semi-colon on line 23 (ok = (vowel == vowels[ i ]))

Also, change vowel from int to char.
Thank you so much Kooth and Coder777. I really appericate the help. I forgot that I could use bools :P
Topic archived. No new replies allowed.