I need the user to be asked to enter a string. The program should display this menu: A count the number of vowels in the string B Count the number of consonants in the string C count both the vowels and consonants in the string D enter another string E exit the program. The program should repeat until the user selects E I already asked this quesiton but I would like the menu not to show up mutlibe times. I would like the menu to show up A-E once not with 10 diffrent outputs.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help writing a C++ code. The code needs to count the number of consonants appearing in the string and return that number.

I need the user to be asked to enter a string. The program should display this menu:
A count the number of vowels in the string
B Count the number of consonants in the string
C count both the vowels and consonants in the string
D enter another string
E exit the program.
The program should repeat until the user selects E
I already asked this quesiton but I would like the menu not to show up mutlibe times. I would like the menu to show up A-E once not with 10 diffrent outputs.

#include<iostream>
using namespace std;
//method to check a char is vowel or not
bool checkVowel(char c){
 if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
 return true;//returning true if it is vowel
 return false;//returning false if it is not vowel
}
//method to check a char is consonant or not
bool checkConsonant(char c)
{
 if(!checkVowel(c) &&( (65<=c&&c<=91) || (97<=c&&c<=122)))//if it is not vowel and it is alphabet then it is consonent
 return true;
 return false;
}
//method to display menu
void menu(){
 cout<<"A count the number of vowels in the string\n";
 cout<<"B Count the number of consonants in the string\n";
 cout<<"C count both the vowels and consonants in the string\n";
 cout<<"D enter another string\n";
 cout<<"E exit the program.\n";
}
int main(){
 //declaring string variable
 string str;
 //reading string from user
 cout<<"Enter a string:";
 cin>>str;
 while(true){
  //now displaying menu
  menu();
  //now reading option from menu
  char c ;
  cout<<"Enter option:";
  cin>>c;
  if(c=='A')
  {
   //counting vowels
   int count=0;
   for(int i=0;str[i]!='\0';i++)
   if(checkVowel(str[i]))
   count++;//if vowel then increasing count
   //now displaying vowel count
   cout<<"Count of vowels:"<<count<<endl; 
  }
  else if(c=='B')
  {
   //counting consonants
   int count=0;
   for(int i=0;str[i]!='\0';i++)
   if(checkConsonant(str[i]))
   count++;//if consonant then increasing count
   //now displaying consonant count
   cout<<"Count of consonants:"<<count<<endl;  
  }
  else if(c=='C')
  {
   int countV=0,countB=0;
   for(int i=0;str[i]!='\0';i++){
   if(checkVowel(str[i]))
   countV++;//if vowel then increasing count
   if(checkConsonant(str[i]))
   countB++;//if consonant then increasing count
   }
   //now displaying vowel count
   cout<<"Count of vowels:"<<countV<<endl; 
   //now displaying consonant count
   cout<<"Count of consonants:"<<countB<<endl; 
  }
  else if(c=='D')
  { 
   //reading another string
   cout<<"Enter another string:";
   cin>>str; 
  }
  else if(c=='E')
  break;//stopping if E is entered
 }
 
 return 0;
}

Expert Solution
Step 1

#include<iostream>
using namespace std;
//method to check a char is vowel or not
bool checkVowel(char c){
 if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
 return true;//returning true if it is vowel
 return false;//returning false if it is not vowel
}
//method to check a char is consonant or not
bool checkConsonant(char c)
{
 if(!checkVowel(c) &&( (65<=c&&c<=91) || (97<=c&&c<=122)))//if it is not vowel and it is alphabet then it is consonent
 return true;
 return false;
}
//method to display menu
void menu(){
 cout<<"A count the number of vowels in the string\n";
 cout<<"B Count the number of consonants in the string\n";
 cout<<"C count both the vowels and consonants in the string\n";
 cout<<"D enter another string\n";
 cout<<"E exit the program.\n";
}
int main(){
 //declaring string variable
 string str;
 //reading string from user
 cout<<"Enter a string:";
 getline(cin,str);  // to read white spaces as well
  //now displaying menu
  menu();
  char c = '#';
 while(c != 'E'){ // menu will not be displayed and program will exit on entering E
 
  //now reading option from menu
  
  cout<<"Enter option:";
  cin>>c;
  if(c=='A')
  {
   //counting vowels
   int count=0;
   for(int i=0;str[i]!='\0';i++)
   if(checkVowel(str[i]))
   count++;//if vowel then increasing count
   //now displaying vowel count
   cout<<"Count of vowels:"<<count<<endl; 
  }
  else if(c=='B')
  {
   //counting consonants
   int count=0;
   for(int i=0;str[i]!='\0';i++)
   if(checkConsonant(str[i]))
   count++;//if consonant then increasing count
   //now displaying consonant count
   cout<<"Count of consonants:"<<count<<endl;  
  }
  else if(c=='C')
  {
   int countV=0,countB=0;
   for(int i=0;str[i]!='\0';i++){
   if(checkVowel(str[i]))
   countV++;//if vowel then increasing count
   if(checkConsonant(str[i]))
   countB++;//if consonant then increasing count
   }
   //now displaying vowel count
   cout<<"Count of vowels:"<<countV<<endl; 
   //now displaying consonant count
   cout<<"Count of consonants:"<<countB<<endl; 
  }
  else if(c=='D')
  { 
   //reading another string
   cout<<"Enter another string:";
   cin>>str; 
  }
  else if(c=='E')
  break;//stopping if E is entered
 }
 
 return 0;
}

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education