943,960 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 923
  • C++ RSS
Apr 24th, 2009
0

Using a sentinal controlled while loop

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
astonomer is offline Offline
1 posts
since Apr 2009
Apr 24th, 2009
0

Re: Using a sentinal controlled while loop

We need code tags and more explanation.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 24th, 2009
0

Re: Using a sentinal controlled while loop

...and try to avoid comments like this
C++ Syntax (Toggle Plain Text)
  1. a += 1; // add 1 to a
It's not old good Cobol language and your program readers are not (all) idiots (I hope )...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 24th, 2009
0

Re: Using a sentinal controlled while loop

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:

C++ Syntax (Toggle Plain Text)
  1. if (isVowel(x))
  2. {
  3. //do stuff
  4. }

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:

C++ Syntax (Toggle Plain Text)
  1. bool isVowel(char cX)
  2. {
  3. cX = tolower(cX);
  4. return (cX == 'a' || cX == 'e' || cX == 'i' || cX == 'o' || cX == 'u');
  5. }
Last edited by skatamatic; Apr 24th, 2009 at 7:53 pm. Reason: More info added
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
780 posts
since Nov 2007
Apr 24th, 2009
0

Re: Using a sentinal controlled while loop

Simple sentinel loop:

C++ Syntax (Toggle Plain Text)
  1. cin >> x;
  2. while (cin.fail() || cin.rdbuf()->in_avail() > 2)
  3. {
  4. cin.ignore(cin.rdbuf()->in_avail());
  5. cin.clear();
  6. cout << "Invalid input\n";
  7. cin >> x;
  8. }
Last edited by skatamatic; Apr 24th, 2009 at 7:57 pm.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
780 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: check if int is actually an int
Next Thread in C++ Forum Timeline: graphics project about drawing circle in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC