I am creating a program to count the vowels and have run into a problem concerning my loop.


}#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

bool isVowel(int num);

int main()
{
bool tof = false; //initializes tof bool to false
int vowels = 0;
char x;

cout << "Please enter a letter to see if it is a vowel" << endl << endl; // Asks the user to enter a letter

cin >> x;

tof = isVowel (x);

if (tof != 0){

}

system("PAUSE");
return 0;
}

bool isVowel(int num)

{

if ('A' == num || 'a' == num ||'E' == num || 'e' == num ||'I' == num ||'i' == num ||'O' == num || 'o' == num ||'U' == num || 'u' == num)
{
return true;

}

else return false;
}

Recommended Answers

All 4 Replies

We need code tags and more explanation.

...and try to avoid comments like this

a += 1; // add 1 to a

It's not old good Cobol language and your program readers are not (all) idiots (I hope ;))...

I think the main problem with your loop is...you don't have a loop.

Also...when checking the value of a bool it is better practice to either compare it to a bool value or just the actual bool itself. Remember a condition (something that you supply an if statement with) IS a bool, so just the bool would suffice:

if (isVowel(x))
{
    //do stuff
}

You are also passing in a int to your function, when I'm pretty sure you meant to pass a char. Not that there's a real difference in the storage of it, there is in the functionality of it. And another tip; to avoid having to use both upper and lower case validation, you could just make use of the tolower() function:

bool isVowel(char cX)
{
cX = tolower(cX);
return (cX == 'a' || cX == 'e' || cX == 'i' || cX == 'o' || cX == 'u');
}

Simple sentinel loop:

cin >> x;
while (cin.fail() || cin.rdbuf()->in_avail() > 2)
{
    cin.ignore(cin.rdbuf()->in_avail());
    cin.clear();
    cout << "Invalid input\n";
    cin >> x;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.