Display Help!

I'm new to programming but I have figured out how I could determine the number of vowels and consonants but I do not know how to display them. I don't know how to start.

The output should be something like this:

Digital Data 
The vowel/s is/are "iiaaa" and its total is 5.
The consonant/s is/are "dgtldt" and its total is 9.

What I've done 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
#include <iostream> 
#include <string>

using namespace std; 

int main()
{
int x[256]={0}; 
int vow=0, con=0;
char input[256]; 

cin.getline(input,256); 
cout<<endl; 

for(int c=0;input[c]!='\0';c++) 
{
++x[input[c]]; 
}

vow=x['A']+x['a']+x['E']+x['e']+x['I']+x['i']+x['O']+x['o']+x['U']+x['u'];
cout<<"The vowel/s is/are "<< <<" and its total is"<<" vow."<<endl<<endl;

con=x['B']+x['b']+x['C']+x['c']+x['D']+x['d']+x['F']+x['f']+x['G']+x['g']+x['H']+x['h']+x['J']+x['j']+x['K']+x['k']+x['L']+x['l']+x['M']+x['m']+x['N']+x['n']+x['P']+x['p']+x['Q']+x['q']+x['R']+x['r']+x['S']+x['s']+x['T']+x['t']+x['V']+x['v']+x['W']+x['w']+x['X']+x['x']+x['Y']+x['y']+x['Z']+x['z'];
cout<<"The consonant/s is/are "<< <<" and its total is"<<" vow."<<endl;

system("PAUSE");

return 0; 
}

Hope you could help me or teach me what to do about that part! Thanks in advance. :)
Last edited on
Hello!
Make an array of your vowels and consonants, so you can iterate throug them. The function tolower(int c) converts every ASCII char to lower case so you don't need to test it twice.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main(int argc, char* argv[]) {
	const int vowals[] = {'a','e', 'i','o','u', 0}; // vowels constant with an ending 0
	char str[] = "Hallo"; // example string

	int count = 0;
	int i = 0;
	int j = 0;
	while(str[i] != 0) { // iterate throug the given string until reaching the null terminator of the string
		while(vowals[j] != 0) { // iterate throug your vowels array until reaching the 0 terminater at the end of the array
			if(tolower(str[i]) == vowals[j]) // convert char at pos i to lower case and compare it with our current vowel
				count++; // increment amount of founded vowels
			j++; // increment vowel loop iterator
		}
		j = 0; // reset vowel loop iterator
		i++; // increment our string iterator
	}

	std::cout << "word: " << str << std::endl;
	std::cout << "vowals: " << count << std::endl;

	return 0;
}


Edit:
english != my native language
sorry for spelling errors -.-*

more comments to code added.
Last edited on
Thank you for your immediate response. I have already done the counting of the vowels and consonants, however I'm having a hard time displaying them out.

My output so far looks like this:

Digital Data 
The vowel/s is/are "        " and its total is 5.
The consonant/s is/are "       " and its total is 9.

What I don't get mainly is how I would extract the vowels and the consonants from the inputted word. The output should be like the following:

Digital Data 
The vowel/s is/are "iiaaa" and its total is 5.
The consonant/s is/are "dgtldt" and its total is 9.

...where it extracts the vowels/consonants and display them accordingly.
Last edited on
Oops, misunderstood. Collect the characters with a std::string like:
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 <iostream>

int main(int argc, char* argv[]) {
	const int vowals[] = {'a','e', 'i','o','u', 0};
	char str[] = "Computer";
	std::string svowels = "";

	int count = 0;
	int i = 0;
	int j = 0;
	while(str[i] != 0) {
		while(vowals[j] != 0) {
			if(tolower(str[i]) == vowals[j]) {
				svowels += vowals[j];
				count++;
			}
			j++;
		}
		j = 0;
		i++;
	}

	std::cout << "word: " << str << std::endl;
	std::cout << "count vowals: " << count << std::endl;
	std::cout << "vowels: " << svowels << std::endl;

	return 0;
}
Thank you very much for your help! I'll try to implement your approach but I still need to do a lot of practice and reading. Thanks again!
Last edited on
Topic archived. No new replies allowed.