Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program which accepts a line of text entered by the user. The whole

ID: 669511 • Letter: W

Question

Write a C++ program which accepts a line of text entered by the user. The whole message must not be stored in the program – instead, it must be read a character at a time, and as each character is read certain properties of the character observed and remembered for later reporting. After the text is read in, the user must be given a choice of these options: 1. Display length of message 2. Display number of non-letter characters (not including the newline character that ends the input) 3. Display numbers of each vowel (upper and lower case counted together) 4. Display numbers of upper and lower case letters 5. Quit The result of the user’s selection must be displayed, and the menu repeated, until 5 for Quit is chosen. Requirements: • Name the source file for your program program4.cpp • Read in the text entered by user using cin.get in a loop something like this: do { cin.get(current_symbol); // check properties of current_symbol and add up various counts as needed here... } while (current_symbol != ’ ’); Note: The purpose of using cin.get(current symbol); rather than cin >> current symbol; is so that spaces will not be skipped over, but rather treated just like any other character. • If an empty message is entered, the user should be prompted to enter a non-empty message until they enter one which is at least 1 character long. (Any character that is not a ’ ’ would count, even a space.) • In counting the numbers of vowels, both upper and lower case letters should be counted together (e.g. for “Edible egg” the count for ‘e’ should be 3). • Use comments to show the main steps in the program, and to document variable declarations.

Explanation / Answer

Program:

# include <iostream>

using namespace std;

int main()
{
   char current_symbol;
   string message="";
   int non_letters=0;
   int vowels=0;
   int upper=0;
   int lower=0;
   int choice=0;
   cout<<" Enter a line of text <press enter>:";
   do
   {
   cin.get(current_symbol);
      // check properties of current_symbol and add up various counts as needed here...
   if(isalnum(current_symbol))
   {
      if(current_symbol=='a'||current_symbol=='e'||current_symbol=='i'||current_symbol=='o'||current_symbol=='u'||
          current_symbol=='A'||current_symbol=='E'||current_symbol=='I'||current_symbol=='O'||current_symbol=='U')
              vowels++;
      if (isalpha(current_symbol) && current_symbol >= 'A' && current_symbol <= 'Z')
           upper++;
       else
           lower++;
   }
   else
   {
      non_letters++;
   }
   message.append(1,current_symbol);
   if(message.size()==1)
   {
      cout<<" Enter atleast one character:";
      cin.get(current_symbol);
message=current_symbol;
   }
   } while (current_symbol!=' ');
     
   cout<<" Message:"<<message;
     
   while(choice<=5)
   {
   cout<<" Choose any option:";
   cout<<" 1. Display length of message:";
   cout<<" 2. Display number of non-letter characters:";//(not including the newline character that ends the input)
   cout<<" 3. Display numbers of each vowel (upper and lower case counted together):";
   cout<<" 4. Display numbers of upper and lower case letters:";
   cout<<" 5. Quit:";
   cout<<" Enter your choice:";
cin>>choice;
switch(choice)
{
    case 1: cout<<" Length of the message:"<<message.size();
    break;
    case 2:
       cout<<" Number of non-letter characters in the message:"<<non_letters;
    break;
    case 3: cout<<" Number of vowels in the message:"<<vowels;
    break;
    case 4: cout<<" Number of uppercase and lower case letters:"<<upper<<" "<<lower;
    break;
    case 5: cout<<" Quitting:";
    exit(0);
   }
   }
   return 0;
}

Output:


Enter a line of text <press enter>:

Enter atleast one character:Welcome to C++!!!. This is Counting Program

Message:Welcome to C++!!!. This is Counting Program

Choose any option:
1. Display length of message:
2. Display number of non-letter characters:
3. Display numbers of each vowel (upper and lower case counted together):
4. Display numbers of upper and lower case letters:
5. Quit:
Enter your choice:1

Length of the message:44
Choose any option:
1. Display length of message:
2. Display number of non-letter characters:
3. Display numbers of each vowel (upper and lower case counted together):
4. Display numbers of upper and lower case letters:
5. Quit:
Enter your choice:2

Number of non-letter characters in the message:14
Choose any option:
1. Display length of message:
2. Display number of non-letter characters:
3. Display numbers of each vowel (upper and lower case counted together):
4. Display numbers of upper and lower case letters:
5. Quit:
Enter your choice:3

Number of vowels in the message:11
Choose any option:
1. Display length of message:
2. Display number of non-letter characters:
3. Display numbers of each vowel (upper and lower case counted together):
4. Display numbers of upper and lower case letters:
5. Quit:
Enter your choice:4

Number of uppercase and lower case letters:4 26
Choose any option:
1. Display length of message:
2. Display number of non-letter characters:
3. Display numbers of each vowel (upper and lower case counted together):
4. Display numbers of upper and lower case letters:
5. Quit:
Enter your choice:5

Quitting:
--------------------------------
Process exited after 48.67 seconds with return value 0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote