| | |
Using a sentinal controlled while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
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;
}
}#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;
}
...and try to avoid comments like this
It's not old good Cobol language and your program readers are not (all) idiots (I hope
)...
C++ Syntax (Toggle Plain Text)
a += 1; // add 1 to a
)... •
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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:
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:
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)
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:
C++ Syntax (Toggle Plain Text)
bool isVowel(char cX) { cX = tolower(cX); return (cX == 'a' || cX == 'e' || cX == 'i' || cX == 'o' || cX == 'u'); }
Last edited by skatamatic; Apr 24th, 2009 at 7:53 pm. Reason: More info added
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Simple sentinel loop:
C++ Syntax (Toggle Plain Text)
cin >> x; while (cin.fail() || cin.rdbuf()->in_avail() > 2) { cin.ignore(cin.rdbuf()->in_avail()); cin.clear(); cout << "Invalid input\n"; cin >> x; }
Last edited by skatamatic; Apr 24th, 2009 at 7:57 pm.
![]() |
Similar Threads
- helllp with loops (C++)
Other Threads in the C++ Forum
- Previous Thread: check if int is actually an int
- Next Thread: graphics project about drawing circle in C++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






