Using a sentinal controlled while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 1
Reputation: astonomer is an unknown quantity at this point 
Solved Threads: 0
astonomer astonomer is offline Offline
Newbie Poster

Using a sentinal controlled while loop

 
0
  #1
Apr 24th, 2009
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Using a sentinal controlled while loop

 
0
  #2
Apr 24th, 2009
We need code tags and more explanation.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Using a sentinal controlled while loop

 
0
  #3
Apr 24th, 2009
...and try to avoid comments like this
  1. a += 1; // add 1 to a
It's not old good Cobol language and your program readers are not (all) idiots (I hope )...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Using a sentinal controlled while loop

 
0
  #4
Apr 24th, 2009
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:

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Using a sentinal controlled while loop

 
0
  #5
Apr 24th, 2009
Simple sentinel loop:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC