Need help with Switch

I need to write a program that counts the number of vowels in a string. The string is written by the user. I know that the best way to do this is with Switch, however I couldn't figure out how to do it because switch would need to take a char as an argument and have the vowels as cases. I made the program work with many"if" statements as an alternative but I want to understand and make it work with switch.

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
/* A program that counts the number of vowels in a string */

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string phrase;
	int nbchar, i, vowel;
	vowel = 0;
		
	cout << "Write a phrase: " << endl;
	getline(cin, phrase);

	nbchar = phrase.size();


		for (i = 0; i < nbchar; i++)
		{
			if (phrase[i] == 'a')
				vowel++;
			if (phrase[i] == 'i')
				vowel++;
			if (phrase[i] == 'e')
				vowel++;
			if (phrase[i] == 'u')
				vowel++;
			if (phrase[i] == 'o')
				vowel++;
			if (phrase[i] == 'y')
				vowel++;
		}

		cout << "In this phrase there is " << vowel << " vowels" << endl;

	return 0;
}


If someone could change my code with switch, it would be much appreciated as I spent many hours trying to figure out how to make it work but failed.
In my opinion it is better to write program without switch.

You should declare a string that will contain vowels. For example,

std::string vowels( "aeiouy" );

and then use your loop

1
2
3
4
5
6
7
int count = 0;
for ( i = 0; i < nbchar; i++ )
{
   if ( vowels.find( phrase[i] ) != std:;string::npos ) count++;
}

cout << "In this phrase there is " << vowel << " vowels" << endl;



With switch your code will look like

switch ( phrase[i] ) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': count++; }
Last edited on
When you use the subscript operator [] in front of a string variable you get an expression which accesses a single character in the string (the expression will evaluate to a char). So while it would be illegal to use switch( phrase ), it's perfectly OK to use switch( phrase[i] )
Last edited on
Or you could also use a "more logical" approach...

1
2
3
4
5
6
7
8
9
10
11
12
		for (i = 0; i < nbchar; i++)
		{
			if ( (phrase[i] == 'a') ||
				(phrase[i] == 'i') ||
				(phrase[i] == 'e') ||
				(phrase[i] == 'u') ||
				(phrase[i] == 'o') ||
				(phrase[i] == 'y') )
			{
				vowel++;
			}
		}


or with a temp var

1
2
3
4
5
6
7
8
9
10
11
		for (i = 0; i < nbchar; i++)
		{
			char ch = phrase[i];

			if ( (ch == 'a') ||  (ch == 'i')
				|| (ch == 'e') || (ch == 'u')
				|| (ch == 'o') || (ch == 'y') )
			{
				vowel++;
			}
		}


Whatever approach you take, I think you should factor out your test into a little helper function so it behaves and looks like the stdlib isalpha(), isdigit(), isspace(), ... functions.

Andy
Note that it is more common to format switch statements vertically.

1
2
3
4
5
6
7
8
9
switch ( phrase[i] ) {
	case 'a':
	case 'e':
	case 'i':
	case 'o':
	case 'u':
	case 'y':
		 count++; 
}


In particular, the condition and the code it triggers should never be on the same line, as that stuffs any chance of setting a meaningful breakpoint (debuggers like gdb and vc++ only understand filepath + line number).

Also note that this is one of the few cases where fall through (due to the lack of break statements) is safe to use.
Thanks for help, I changed my code and it works like a charm now.

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
/* A program that counts the number of vowels in a string */

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string phrase;
	int nbchar, i, vowel;
	vowel = 0;
		
	cout << "Write a phrase: " << endl;
	getline(cin, phrase);

	nbchar = phrase.size();

	for (i = 0; i < nbchar; i++)
	{
		switch ( phrase[i] ) 
		{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
			case 'y':

			 vowel++; 
		}
	}
		cout << "In this phrase there is " << vowel << " vowels" << endl;

	return 0;
}
Topic archived. No new replies allowed.