Vowel Counter with Function

I cant seem to figure out a way to end the do while loop, rest of the code seems to work fine. The current way I end the loop isn't recognized.

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

bool isVowel(char ch);

int main() {

  char letters;
  cin >> letters;
  
  int count;
  do {
    count = 0; 
    do {
      cout << "Enter characters: "; cin >> letters;
      if(isVowel(letters)) count++;
    }
    while (letters != 'n');
  }
  cout << "There are " << count << " vowels in this sentence." << endl;
}


bool isVowel(char ch) {
  switch (ch) {
    case 'A':
    case 'a':
    case 'E':
    case 'e':
    case 'I':
    case 'i':
    case 'O':
    case 'o':
    case 'U':
    case 'u':
      return true;
      break;

    default:
      return false;
      break;
  }
}
Last edited on
You've got two do-while loops. What's the outer one for?

1
2
3
4
5
6
7
8
do {
    count = 0; 
    do {
      cout << "Enter characters: "; cin >> letters;
      if(isVowel(letters)) count++;
    }
    while (letters != 'n');
  } // WHERE'S THE WHILE? 


Not sure why I had an extra do statement, should have only been one...
Anyway, that's the part I am stuck at, I can't figure out how to end the loop.
I can't figure out how to end the loop.


Enter "n" as the letter.
Whoops that's supposed to be a \n, I'm trying to get the loop to end after the first line is inputted. My current code 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>
using namespace std;

bool isVowel(char ch);

int main() {

  char letters;
  int count = 0;
  
  do {
    cout << "Enter characters: "; cin >> letters;
    if(isVowel(letters)) count++;
      cout << "There are " << count << " vowels in this sentence." << endl;
      }
    while (letters != '\n');
}


bool isVowel(char ch) {
  switch (ch) {
    case 'A':
    case 'a':
    case 'E':
    case 'e':
    case 'I':
    case 'i':
    case 'O':
    case 'o':
    case 'U':
    case 'u':
      return true;
      break;

    default:
      return false;
      break;
  }
}

I'm very new at programming in general, I just can't figure out a way to loop the counting correctly.
Last edited on
the cin stream operator (<<) ignores whitespace, and \n is one of the letters ignored.

you can do something like ask for a non-letter to quit, like '!' or the like. that is an option that may be good enough to get it done.

you can make a second layer, eg
enter a letter
count if its a vowel
ask if want to stop (read a letter, check for 'y' or something)
(two distinct cin statements, one to control the loop, one as data to count)

that might look like
1
2
3
4
5
6
7
8
9
char quit; 
 do {
    cout << "Enter characters: "; cin >> letters;
    if(isVowel(letters)) count++;
      cout << "There are " << count << " vowels in this sentence." << endl;
   cout << "do you want to quit?\n";  
   cin >> quit;      
}    while ( quit != 'y');



there are other ways to deal with it too, but maybe one of those gets you going?


return true;
break; <-- you already returned, it can't get here.... it does not hurt, of course.

Last edited on
Thanks for the explanation, unfortunately this question won't accept any input to quit the loop, the input for this assignment is exactly "The dog is fast." and "Euouae is the longest English word consisting of all vowels." I initially had a similar exit for the loop, but it can't take any other input besides those two sentences.
do you have exact instructions as to what is put in to quit?
with that input you can stop on '.'
YES, that works, this stupid assignment took took it! Thank you, can't believe I didn't think of that...
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 <iostream>
using namespace std;

bool isVowel(char ch);

int main() {

  char letters;
  int count = 0;
  cout << "Enter characters: " << endl;
  
  do {
    cin >> letters;
    if(isVowel(letters)) count++;
  }  while (letters != '.');
    cout << "There are " << count << " vowels in this sentence." << endl;
  return 0;
}


bool isVowel(char ch) {
  switch (ch) {
    case 'A':
    case 'a':
    case 'E':
    case 'e':
    case 'I':
    case 'i':
    case 'O':
    case 'o':
    case 'U':
    case 'u':
      return true;
      break;

    default:
      return false;
      break;
  }
}
Thank you, can't believe I didn't think of that...

you will get there. Its part of the zen of working smarter, not harder :P Comes down to, the more you know about your data, the more you can exploit it. What we did isnt really useful apart from solving the GIGO of one specific task, but sometimes, that is enough. Fixing it for all strings is more work, if you get bored, you can take a crack at it.

Glad it worked!
Last edited on
Topic archived. No new replies allowed.