I'm just learning about using Bool in class, and I need to know why my source code doesn't output correctly. My goal is to create a program that determines whether an inputted letter is a Vowel or not. My code outputs every letter as a Vowel. What am I doing wrong?
Source Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int Let;
bool x;
int main() {
cout << "Please enter a letter: " << endl;
cin >> Let;
cout << endl;
if (Let == 'a' || 'e' || 'i' || 'o' || 'u')
x = true;
else
x = false;
if (x == true)
cout << "This is a Vowel!" << endl;
else
cout << "This is not a Vowel!" << endl;
Note: I can write a simple code without a Boolean variable, and it will work just fine, but our instructor wants us to use a Boolean variable. What am I not understanding here?
First, a comparison like Let=='a' evaluates to either true or false. || returns true if one of its two(!) boolean operands is true. That should tell you why if (Let == 'a' || 'e' || 'i' || 'o' || 'u') is wrong.
Second, since these expressions already give you boolean values, it's pointless writing something like if (foo)x=true; else x=false;. You can just write x=foo;
Third, if takes a boolean value as the condition. Therefore, x==true is redundant, as the comparison yields true if x is true and false otherwise. It should be just if (x).
So if [Let=='a' || etc...] is a True/False decision, then I only need to leave that part of the code.
Getting rid of the other pointless statements, as you suggested, led me to this:
int main() {
cout << "Please enter a letter: " << endl;
cin >> Let;
cout << endl;
(Let == 'a' || 'e' || 'i' || 'o' || 'u');
if (Let)
cout << "This is a Vowel!" << endl;
else
cout << "This is not a Vowel!" << endl;
return 0;
}
But it's now outputting that all of my letters are not vowels. :\
I'm really confused now.
Now integral values that are non-zero ('e' is such a number) are converted to true in a boolean context. So false || 'e' becomes false || true, which is true. It follows:
How would I go about writing a program that counts how many Vowels are in a word? I honestly have no clue where to start on this one, specifically with how to accept an inputted word rather than a single character or integer.
There's one more issue, namely those two global variables. Don't use global variables unless you really need to. Remove the global declaration of isVowel (it is shadowed by the local one anyway) and move the declaration of Let into main().
std::string (or just string when you have an appropriate using directive).
Note that it's not a primitive type, so you need to #include <string>.
The member function length() gives you the length of the string and operator[] allows you to access a specific character in the string. Here's the complete reference: http://www.cplusplus.com/reference/string/string/
Okay, using the information from my first program, I've come a ways in creating the Vowel Counting program. I've gotten about 75% of it, but I'm missing something, and I'm not sure how to implement it. (Note: I had to make Vcount a global declaration to make it work in both functions.) What I need is a way for the program to check each letter in the word in Process01().
Here's what I have so far:
// Program to Count the Vowels in a Word.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int Process01();
int Vcount;
cout << "There are " << Vcount << " vowels in your word." << endl;
return 0;
}
int Process01(){
char Let;
// Some sort of code to identify which letter to check
bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
if (int i=0;i<Word.length();i++)
{
char c=World[i];
//do stuff with c
}
(Note: I had to make Vcount a global declaration to make it work in both functions.
That's not necessary. To pass values into a function, use parameters and to return a value from a function, use the return value (instead of returning a pointless 0).
No matter which function (main or process1) that I declare Vcount in, even if I return Vcount, it lists Vcount as unidentified in the opposing function.
So it's either not doing what you say it can, or I'm misunderstanding something.
You are, you should look up functions again. They take a number of parameters and return a value.
For example, the mathematical function cos takes one value and returns one. cos(x) evaluates to the return value (the cosine of x).
If you don't do anything with the return value in main() (like assigning it to a variable), it is simply discarded.
Assigning Vcount to Global: It works in Main and Process1.
Assigning Vcount to Main: Works in Main; Unidentified in Process1.
Assigning Vcount to Process1: Works in Process 1; Unidentified in Main.
Both Main and Process1 make use of Vcount.
How else, other than assigning it to a Global Declaration, can I get it to work?