Counting vowels HW help

The assignment is: Write a program that reads in text one character at a time and counts the
number of vowels ’a’, ’e’, ’i’, ’o’ and ’u’ in the text. Both lower case and upper case vowels
should be counted together. Hint: you may find it useful to use five separate counters. When a
period or question mark appears in the input text, the program prints the number of each vowel
and halts.

and my code so far is

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
#include<iostream>
#include<cmath>
using namespace std;

main ()
{
  int a(0);
  int e(0);
  int i(0);
  int o(0);
  int u(0);
  char c;

  cout<<"Enter text (ending in '.' or '?'): ";
  cin>>c;

  while(c!='.' && c!='?')
    {
if (c == 'a'|| 'A')
  {a++;}
if (c == 'e' || 'E')
  {e++;}
if (c == 'i' || 'I')
  {i++;}
if (c == 'o' || 'u')
  {o++;}
if (c == 'u' || 'U')
  {u++;}
    }

  cout<<"Number of a's: "<<a;
  cout<<"Number of e's: "<<e;
  cout<<"Number of i's: "<<i;
  cout<<"Number of o's: "<<o;
  cout<<"Number of u's: "<<u;
 

 return 0;
}


If anyone could help me or give me hints that would be great.
Using the && operator will never get the program to stop, so one letter must be ? and . at the same time... use the || operator.

This:
1
2
if (c == 'a'|| 'A')
{a++;}

Can be replaced with this:
1
2
if (c == 'a'|| 'A')
a++;


I don't know whether the loop you used would work or not however you can do this instead for the loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

string c;

  cout << "Enter a string of text (ending in '.' or '?'): ";
  cin >> c;


while(c.at[x] != '.' || c.at[x] != '?'  || c.at[x] != '!' ){

if (c == 'a'|| 'A')
  a++;
if (c == 'e' || 'E')
  e++;
if (c == 'i' || 'I')
  i++;
if (c == 'o' || 'u')
  o++;
if (c == 'u' || 'U')
  u++;

x++;

}


.at[x] checks at x location in the string so you can do this:
-----x
Chips?

Oppose to:
- - - - - x
C h i p s ?
For each charactar
Last edited on
Topic archived. No new replies allowed.