Writing a value-returning function.

Nov 9, 2020 at 6:19am
Hello, coders. I have another puzzling code for ya (could be simple). Here I have an almost complete program. The issue I have is appointing:
"G is a vowel: 0"
"p is a vowel: 0"

I would really appreciate it if you could explain how this works. As you can see, I have everything set up but these values. Thank you for your time coders.

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
  
#include <iostream>

using namespace std;

bool isVowel(char letter);

int main()
{
    char userChar;

    cout << "Please enter a character: " << endl;
    cin >> userChar;
    cout << endl;

    if (isVowel(userChar))
        cout << userChar << " is a vowel: 1" << endl;
    else
        cout << userChar << " is not a vowel: 0" << endl;

    return 0;
}

bool isVowel(char letter)
{
    switch (letter)
    {
        case 'a':
        case 'E':
        case 'e':
        case 'G':
        case 'g':
        case 'I':
        case 'i':
        case 'o':
        case 'P':
        case 'p':
        case 'U':
        case 'u':
            return true;
        default:
            return false;
    }
}
Nov 9, 2020 at 6:45am
G and P are in your list. so they return 'true'. The computer does not know they are not vowels, it just knows you put them in the switch statement.
or are you asking something else?
Last edited on Nov 9, 2020 at 6:46am
Nov 9, 2020 at 8:20am
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>
#include <cctype>
#include <cstring>

using namespace std;

bool isVowel(char letter);

int main()
{
	char userChar;

	cout << "Please enter a character: ";
	cin >> userChar;

	cout << '\n' << userChar << " is " << (isVowel(userChar) ? "" : "not ") << "a vowel\n";
}

bool isVowel(char letter)
{
	static const char* const vowels = "aeiou";

	return strchr(vowels, tolower(letter)) != NULL;
}

Nov 9, 2020 at 2:15pm
Thank you for your feedback seeplus and jonnin. The program is asking when printing the value of a bool, true will be displayed as 1 and false will be displayed as 0.
Nov 10, 2020 at 1:59am
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
#include <iostream>
#include <cctype>   // ::tolower
// http://www.cplusplus.com/reference/cctype/

bool IsVowel(char);
bool IsOdd(char);

int main()
{
   std::cout << "Input a single character: ";
   char input;
   std::cin >> input;
   std::cout << '\n';

   // let's check if the input is a letter or a numeric digit

   if (input >= 'A' && input <= 'z')  // is it a letter?
   {
      if (IsVowel(input))
      {
         std::cout << input << " is a vowel: 1 (true)\n";
      }
      else
      {
         std::cout << input << " is a vowel: 0 (false)\n";
      }
   }
   else if (input >= '0' && input <= '9')  // is it a numeric digit?
   {
      if (IsOdd(input))
      {
         std::cout << input << " is odd: 1 (true)\n";
      }
      else
      {
         std::cout << input << " is even: 0 (false)\n";
      }
   }
   else // not a letter or numeric digit
   {
      std::cout << input << " not recognized\n";
   }
}

bool IsVowel(char n)
{
   switch (::tolower(n))  // temp convert to lower case
   {
   case 'a': case 'e': case 'i': case 'o':  case 'u': // is it a vowel?
      return true;

   default:  // if not, consonant
      return false;
   }
}

bool IsOdd(char n)
{
   if ((int)(n - '0') % 2)  // temp convert to integer
   {
      return true;   // odd
   }
   else
   {
      return false;  // even
   }
}
Nov 10, 2020 at 10:23am
The program is asking when printing the value of a bool, true will be displayed as 1 and false will be displayed as 0.


The requirements required by some teachers never cease to amaze!

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 <cctype>
#include <cstring>

using namespace std;

bool isVowel(char letter);

int main()
{
	char userChar;

	cout << "Please enter a character: ";
	cin >> userChar;

	const bool isv {isVowel(userChar)};

	cout << '\n' << userChar << " is " << (isv ? "a vowel: " : "not a vowel: ") << isv << '\n';
}

bool isVowel(char letter)
{
	static const char* const vowels = "aeiou";

	return strchr(vowels, tolower(letter)) != NULL;
}

Nov 10, 2020 at 3:16pm
Thank you coders, everything is working great!
Topic archived. No new replies allowed.