0

Im starting to learn about bool operators and decided to write a program to see if a letter is a vowel or not. If its a vowel it returns true, if not returns false. This is the program i've written

#include <iostream>
using namespace std;

bool isletteraVowel(string str) {

bool status = true;

if(str == "A" || str == "E" || str == "I" || str == "O" || str == "U" ) {
status = true;

} else if (str == "C") {
status = false;

}
return status;
}

int main() {

isletteraVowel("C");

}

This problem complies properly, however, it returns nothing. The only output i recieve is

"[Done] exited with code=0 in 3.719 seconds"

Im unsure as it why nothing is returning.

David Ling
  • 145
  • 1
  • 12
  • 6
    What do you expect out of your program? You didn't tell it to display anything, so if that is what you are expecting you need to program that in. – NathanOliver Mar 16 '20 at 21:06
  • 1
    I believe you may be confusing "return" (what a non-void function does when it finishes) with "output" (think `std::cout`). I'd suggest picking up a [good C++ book](https://stackoverflow.com/q/388242/2602718). – scohe001 Mar 16 '20 at 21:07
  • How would you ever know what is being returned by `isletteraVowel("C");` -- you never ***check the return***? Maybe `if (isletteraVowel("C")) std::cout << "Vowel\n"; else std::cout << "Not Vowel\n";` – David C. Rankin Mar 16 '20 at 21:10

1 Answers1

3

The value is returning however you are not printing the result to the screen. You could fix this by changing the program accordingly:

#include <iostream>
using namespace std;

bool isletteraVowel(string str) {

bool status = true;

if(str == "A" || str == "E" || str == "I" || str == "O" || str == "U" ) {
status = true;

} else if (str == "C") {
status = false;

}
return status;
}

int main() {

cout << isletteraVowel("C") << endl;

}

Hope this helps!

  • ah okay i see. So i tried using cout but it returns 0, is that correct? It also returns 1 if the letter is a vowel. – David Ling Mar 16 '20 at 21:14
  • You can use [`std::boolalpha`](https://en.cppreference.com/w/cpp/io/manip/boolalpha) if you want "true" or "false" to print. – Fred Larson Mar 16 '20 at 21:15
  • @DavidLing That's because that's how `std::cout` handles printing Booleans. You could try something like `std::cout << (isletterVowel("C") ? "true" : "false"` or `std::boolalpha` as Fred Larson has suggested. –  Mar 16 '20 at 21:51