function program

Hi,
First year programming here and ran into some trouble that I can't seem to figure out. The purpose of the program is to ask the user to type in a string of letters and use my own function that makes the program sort out each individual word and outputs whether or not it's a vowel. My program works. But when you type in a string such as "Hello" there's an extra line that gets out putted and it just says " is a consonant.". I think it's grabbing a newline somewhere and it has to be ignored by using cin.ignore('/n'). Can't figure it out.

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

#include <cstdlib>
#include <iostream>
bool isVowel (char);

using namespace std;



main(int argc, char *argv[])
{

    char letter;
    do{
    cin.get(letter);
    if (isVowel(letter) > 0)
    {
    cout << letter << " is a vowel." << endl;
    }
    else 
    cout << letter << " is a consonant." << endl;
    }
    while(true);
   
   system("PAUSE");
   return EXIT_SUCCESS;
}

bool isVowel(char func)
{
 
  if(func =='a')
           return 1;
  if(func =='e')
           return 1;
  if(func =='i')
           return 1;
  if(func =='o')
           return 1;
  if(func =='u')
           return 1;
  if(func =='A')
           return 1;
  if(func =='E')
           return 1;
  if(func =='I')
           return 1;
  if(func =='O')
           return 1;
  if(func =='U')
           return 1;
  else
          return 0;
}
Last edited on
Maybe add an additional check before looking at whether letter is a vowel. You could check too see if letter is the endline character '\n'.

or check to see if isVowel(letter) is not only true, but check if it is false that way the '\n' won't get into either of those condition by accident.
I tried that in some test code and couldn't get anything to work. In bool isVowel(char func) I tried if(func == '\n') { cin.clear(); } else{ switch(func){ .....} }, but it still just kept printing the newline character.
I'm still stumped. Haha. Thanks guys,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do{
       cin.get(letter);

        // Check for endline
       if(letter == '\n') 
            cout << "End.\n";
       
       // If isVowel returns true
       else if (isVowel(letter))
            cout << letter << " is a vowel." << endl;

        // If isVowel returns false
       else if(!isVowel(letter))
            cout << letter << " is a consonant." << endl;

       }while(true);


This way the endline does not make it into your vowel or consonant loops.
So I had the check right. I was just doing the check in the wrong part of the code.
Thanks bcrawford. I was able to finish the rest of the program with your help. The purpose of the program was to make it so that a user types in a series of letters and the program would tell you what is a vowel and what is a consonant. Then at the very end it would tell you how many vowels total were typed. I added a counter and was able to complete the program. Thanks!

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

#include <cstdlib>
#include <iostream>


bool isVowel (char);


using namespace std;



main(int argc, char *argv[])
{

    char letter;
    int counter = 0;


    cout << "Please enter in a series of letters and I will tell you" << endl <<
    "how many letters are vowels there are." << endl << endl;
 

    do{
    
    cin.get(letter);
    cout << endl;


    if(letter == '\n') 
    cout << "You have typed in " << counter << " vowels. Thank you." << endl
    << endl;
    
    else if (isVowel(letter) == 1)
    {
    cout << letter << " is a vowel." << endl;
    ++counter;
    
    
    }
    else if (isVowel(letter) == 0)
    cout << letter << " is a consonant." << endl;
    

    }
    while(true);


   system("PAUSE");
   return EXIT_SUCCESS;
}

bool isVowel(char func)
{
 
  if(func =='a')
           return 1;
  if(func =='e')
           return 1;
  if(func =='i')
           return 1;
  if(func =='o')
           return 1;
  if(func =='u')
           return 1;
  if(func =='A')
           return 1;
  if(func =='E')
           return 1;
  if(func =='I')
           return 1;
  if(func =='O')
           return 1;
  if(func =='U')
           return 1;
  else
          return 0;
}

Can I make a suggestion, if you can change it or add it as I'm not sure if this is for school or just a fun app you are doing for yourself. If it was school I didn't know what all you have learned so far. Inside the boolean function instead of all the if statements why not go with a switch statement?

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
switch(func)
{
	case 'a':
		return 1;
	case 'e':
		return 1;
	case 'i':
		return 1;
	case 'o':
		return 1;
	case 'u':
		return 1;
	case 'A':
		return 1;
	case 'E': 
		return 1;
	case 'I':
		return 1;
	case 'O':
		return 1;
	case 'U':
		return 1;
	default:
		return 0;
}	
Yeah the switch case is the way to go. You could even change the character to uppercase to reduce the amount of cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func = toupper(func);

switch(func)
{
	case 'A':
		return 1;
	case 'E': 
		return 1;
	case 'I':
		return 1;
	case 'O':
		return 1;
	case 'U':
		return 1;
	default:
		return 0;
}


Just for clarification, I noticed in one of your earlier posts you were trying to use

cin.clear();

the clear() function is for clearing any errors that may have occurred when using fstreams for input or output ( ex. EOF(), fail(), bad() ) to be able to continue reading or writing. It won't stop the process of reading like you were trying to use it.
Last edited on
You can have many cases to exectute the same statement, and why not use true and false instead of 1 and 0 when the return type is bool?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(func)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E': 
case 'I':
case 'O':
case 'U':
	return true;
default:
	return false;
}
Peter87 is right. True/False is much more readable. Also I botched up on the switch statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func = toupper(func);

switch(func)
{
	case 'A':
	case 'E': 
	case 'I':
	case 'O':
	case 'U':
		return true;
                break;
	default:
		return false;
}


With out the break; it would go through all the cases and would end on false every time.
Yeah I was going off a bad reference I googled for flushing cin and got that. Also break shouldn't make a difference. When the program gets to a return it should automatically jump out of the function or block it is in.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
      cout << "Enter 999: "; 
      int num;
      cin >> num;
      
      if(num == 999)
      {
             cout << "You opted to terminate. Goodbye.";
             return -1;
       }
      
       cout << "Other string literals to output.";
       return 0;
}


It will never go to the last standard output string because of the return. Same with the switch statement. Soon as it hits return it is done, no need for the break;
Last edited on by closed account z6A9GNh0
Good catch BHX. I should have had my coffee before posting this morning. :)
By looking at the name of your variable I assume that you only want to get a letter. cin() gets a letter and a string and the string is a constant so is your "Hello". In that case I would try getchar().
Topic archived. No new replies allowed.