How to grab separate inputs and count the vowels
Feb 18, 2015 at 5:00am UTC
So, I'm trying to figure out how to take a certain amount of inputs and grabbing those inputs and checking to see if any of those characters are vowels. No strings. Please help!!!!
Feb 18, 2015 at 5:02am UTC
Could you explain a bit more? What have you written so far?
What does "No strings" mean? Surely you are not forbidden from using one of the most fundamental classes in C++?
Feb 18, 2015 at 5:05am UTC
Im sorry for not being specific. This 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
#include <iostream>
using namespace std;
int main()
{
char x1, x2, x3, x4, x5, x6;
char answer;
cout << "Enter 6 characters." << endl << endl;
cout << "Enter first character: " ;
cin >> x1;
cout << "Enter second character: " ;
cin >> x2;
cout << "Enter third character: " ;
cin >> x3;
cout << "Enter fourth character: " ;
cin >> x4;
cout << "Enter fifth character: " ;
cin >> x5;
cout << "Enter sixth character: " ;
cin >> x6;
Now, I'm trying to figure out how many are vowels. Should I use a for loop or while loop or if statement???
Last edited on Feb 18, 2015 at 5:06am UTC
Feb 18, 2015 at 5:26am UTC
use for loop for the input.
Feb 18, 2015 at 8:32am UTC
Your best bet is to use a switch statement with one case for each vowel, then default for non-vowel.
Second step is to wrap up everything in a do-while loop to allow users to run the program multiple times to be able to enter chars more than once.
I did not compile this code, but it should work:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include <iostream>
using namespace std;
int main()
{
char input;
char x1, x2, x3, x4, x5, x6
bool done = false ;
do {
cout << "Enter 6 characters" << endl << endl;
cout << "Enter first character: " << endl;
cin >> x1;
cout << "Enter second character: " << endl;
cin >> x2;
cout << "Enter third character: " << endl;
cin >> x3;
cout << "Enter fourth character: " << endl;
cin >> x4;
cout << "Enter fifth character: " << endl;
cin >> x5;
cout << "Enter sixth character: " << endl;
cin >> x6;
int count = 0;
switch (x1) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
switch (x2) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
switch (x3) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
switch (x4) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
switch (x5) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
switch (x6) {
case 'a' : count++; break ;
case 'i' : count++; break ;
case 'e' : count++; break ;
case 'u' : count++; break ;
case 'o' : count++; break ;
}
cout << "Number of vowels is " << count << endl;
cout << "Exit? (Y/N)" ;
cin >> input;
if (input == 'y' ) {
done = true ;
}
} while (!done)
system ("PAUSE" );
return 0;
}
Last edited on Feb 18, 2015 at 8:53am UTC
Feb 18, 2015 at 1:17pm UTC
Or, you could count the occurrences of all characters in a std::map and then just look at how many of each vowel there are. It would be a lot less code.
Feb 18, 2015 at 4:19pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
int main() {
char ch[6];
char vow[6] = {'a' ,'e' ,'i' ,'o' ,'u' };
int count=0;
for (int i=0; i<6; i++){
std::cout << "enter character " << i+1 << ": " ;
std::cin >> ch[i];
ch[i]=tolower(ch[i]);
for (auto x: vow) if (x==ch[i]) count++;
}
std::cout << "total vowels inputed = " << count << "\n" ;
for (auto x: vow) {
count=0;
for (auto y: ch) if (x==y) count++;
if (count) std::cout << x << ": " << count << "times\n" ;
}
return 0;
}
Feb 18, 2015 at 6:56pm UTC
That works, but it's pretty awkward.
Last edited on Feb 18, 2015 at 6:56pm UTC
Feb 19, 2015 at 6:46am UTC
letter counter by map
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
#include <iostream>
#include <map>
using std::cin;
using std::cout;
using std::string;
using std::map;
int main() {
string s;
map<char , int > letters;
cout << "enter a character string: " ;
getline(cin, s);
for (auto &x: s){
x= tolower(x);
++letters[x]; // map operator[]
}
for (auto x: letters){
cout << x.first << " " << x.second << "\n" ;
}
return 0;
}
Mar 7, 2015 at 5:22pm UTC
Thanks everyone for the help!!!!!
Topic archived. No new replies allowed.