Objective: Write void functions, value-returning functions, Boolean functions, u
ID: 3668449 • Letter: O
Question
Objective: Write void functions, value-returning functions, Boolean functions, using cin.get method, if statements, and loops.
Write a C++ program that counts the number of vowels in a text. The program should start by asking the user enter lines of characters terminated by a ‘!’. The program should output the number of vowels. Continue processing lines of characters until the user decides to quit (e.g. ask a y/n question).
For this assignment you must use cin.get() to read a single character. For example, cin.get(ch);
Will read a character from the keyboard and stores it in ch.
Write functions to do each of the following (Required): • A function that return true if a character is a vowel false otherwise. • A function that prints the results.
Note: programs that do not use functions will not be accepted and will get zero points.
Extra credit• Write a function that returns true if a character is a consonant. • Write a function that returns true if a character is a white space. • output the results in the same output function defined above.
Sample input/output: Enter lines of characters terminated by !. A single line of characters. ! There are 9 vowels in the text. Continue (y/n)? y
Explanation / Answer
#include<iostream>
using namespace std;
bool isVowel(char c){
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U') {
return true;
}
else {
return false;
}
}
bool isConstant(char c){
if(!isVowel(c) && ((c>='a' && c<='z')||(c>='A' && c<='Z')))
return true;
else
return false;
}
bool isSpace(char c){
if(c == ' ')
return true;
else return false;
}
void printResult(int count){
cout<<"There are "<<count<<" vowels in the text. Continue (y/n)?"<<endl;
}
int main(){
char c,ch='y';
int count = 0;
cout << "Enter lines of characters terminated by !" << endl;
do{
cout<<"A single line of characters !"<<endl;
c = cin.get();
for(;c != '!';)
{
if(isVowel(c))
count++;
c = cin.get();
}
printResult(count);
cin>>ch;
}while(ch == 'y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.