Question

The menu Here is what the menu should look like when running your program:                              &nbsp

The menu

Here is what the menu should look like when running your program:

                                        ***********************************

                                        *         FUN MENU             *

                                        ***********************************

                                                <S>um of natural integers

                                                <L>eap year check

                                                <C>ount vowels

                                                <E>xit

Menu tasks

  • Sum of natural integers
    • ask users to enter an integer (n), then use for loop to compute the sum of the first n integers and finally display the result. If users enter negative integer display an error message.
  • Leap year check
    • ask users to enter a year, then use if else statements to determine if it's a Leap year and finally display the result.

The following definition is how to verify if a year is a leap year taken from Wikipedia.org:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are. For more information feel free to go to: Leap Year Info (외부 사이트로 연결합니다.)

Note: to get full credit you must use if else statements, not multiple if statements. Read the leap year definition carefully before coding. I will look for efficiency in your if else logic. Less efficient code may get deductions. Remember there aren't many leap years in life so quickly ruling out non-leap years is a good tactics.

  • Count vowels
    • ask users to enter a string to count for the vowels, then use a while loop to count the number of vowels occurrences in the string (vowels are: a,e,i,o,u. You must count both upper case and lower case ones). Users may enter strings with spaces such as "Looping is a powerful construct in programming languages.". Think about efficiency when determining if a character is a vowel (case insensitivity) by assuming you need to do the counting on strings containing 1M of characters or so.
  • Exit
    • simply display the message "The fun is over. Have a nice day!!!" and then invoke exit(0); You must include cstdlib library for this exit library function.

Main function's implementation requirements

  • Previous assignments' requirements are applied.
  • No global variable is needed in this assignment.
  • Main function should contain necessary variables and control structures such as if/else, switch and looping statements.
  • Variables declared in main must be properly initialized with default values before they can be used.
  • Avoid code redundancy in switch statements.
  • In the main function the algorithm for the menu-driven program will be similar to the following:
    • Declare needed variables
    • Use a do while loop
    • The body of the do while loop will contain the following:
      • Show the menu.
      • Ask users to select a choice ('S', 's', 'L', 'l', 'C', 'c', 'E', 'e').
      • Use a switch statement to handle users' selections: if users select 'S' or 's' perform Sum of natural integer task. If users select 'L' or 'l' perform LeapYear check,... (you get the idea). The default case in the switch statement is to handle users' invalid choice which should output a warning message like: "Warning: invalid choice! Enter S,s or L,l or C,c or E,e only please.". Again no redundant code or repeated code should be used for similar user choice such 'E' and 'e', or 'S' and 's', ....

---------------------------------------------------------------------------------------------------------------------------

NOTE: For all input readings that use getline function (Counting Vowels is one) you may need to flush the input stream (still containing left over characters from previous input/keyboard operations) prior to invoking getline by placing the following code just before the getline statement:

                       if (cin.peek ( ) == '\n')  

                              cin,ignore ( ); // flush the '\n' character from input stream

// now use getline to read the entire line including spaces

----------------------------------------------------------------------------------------------------------------------------

Make this for me!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:


#include <iostream>
#include <cstdlib>

using namespace std;

//Function that computes sum of N numbers
void sumN()
{
int n, i, sum=0;
  
//Reading a NUMBER
cout << "\n Enter a number: ";
cin >> n;
  
//Looping
for(i=1; i<=n; i++)
{
//Accumulating sum
sum = sum + i;
}
  
//Printing sum
cout << "\n Sum of " << n << " natural numbers: " << sum << " \n";
}

//Function that checks for leap year
void leapYearCheck()
{
int year;
  
cout << "\n Enter Year: ";
cin >> year;
  
// Exactly divisible by 400
if (year%400 == 0)
cout << year << " is a leap year.\n";
// Exactly divisible by 100 and not by 400
else if (year%100 == 0)
cout << year << " is not a leap year. \n";
// Exactly divisible by 4 and neither by 100 nor 400
else if (year%4 == 0)
cout << year << " is a leap year.\n";
// Not divisible by 4 or 100 or 400
else
cout << year << " is not a leap year. \n";
}

//Function that counts vowels
void countVowels()
{
string str;
int cnt=0, i;
  
//Reading string from user
cout << "\n Enter a string: ";
if (cin.peek ( ) == '\n')
cin.ignore ( ); // flush the '\n' character from input stream
getline(cin, str);
  
//Iterating over each character
for(i=0; i<str.length(); i++)
{
switch(str[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': cnt += 1; break;
}
}
  
cout << "\n\n Total number of vowels: " << cnt << "\n";
}

//Function that prints the menu
void menu()
{
cout << "\n *********************************** ";
cout << "\n * FUN MENU *";
cout << "\n *********************************** ";
cout << "\n \t <S>um of natural integers \n\t <L>eap year check \n\t <C>ount vowels \n\t <E>xit\n";
}

// Main function
int main()
{
char ch;
  
while(1)
{
//Printing menu
menu();
cout << "\n Enter your choice: ";
cin >> ch;
  
//Calling respective function
switch(ch)
{
case 'S':
case 's': sumN(); break;
  
case 'L':
case 'l': leapYearCheck(); break;
  
case 'C':
case 'c': countVowels(); break;
  
case 'E':
case 'e': cout << "\n The fun is over. Have a nice day!!! \n"; exit(0);
}
}
  

return 0;
}

___________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
The menu Here is what the menu should look like when running your program:                              &nbsp
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please  ...

    JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please   Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...

    CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap year Create a new C++ project titled “HW 6” in the CodeBlocks IDE. Use the “Console Application” project option. Over time, the calendar we use has changed a lot. Currently, we use a system called the Gregorian Calendar and it was introduced in 1582. The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible...

  • #1 Write a MATLAB script to solve: Leap Year Program (practice your IF statements!) Write a...

    #1 Write a MATLAB script to solve: Leap Year Program (practice your IF statements!) Write a code to determine if a year is considered a leap year or not? Display to the screen if the year is a leap year or not. Use num2str() and display() or fprintf(). In the Gregorian calendar three criteria must be taken into account to identify leap years: The year is evenly divisible by 4 (Exceptions 1700, 1800, and 1900); If the year can be...

  • Write a program in Python that will: Here are following requirements to completing Menu shapes: 1....

    Write a program in Python that will: Here are following requirements to completing Menu shapes: 1. Create a "MENU" with 4 - 5 options have the user click the option and thereafter ask the user to enter the amount of stars from 1 - 50. From there your "for loop" will create the shape. Menu options need to be: 1. Filled Triangle 2. filled Square 3. This can be any shape such as a Square, circle, a different angle of...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • Textual menus work by showing text items where each item is numbered. The menu would have...

    Textual menus work by showing text items where each item is numbered. The menu would have items 1 to n. The user makes a choice, then that causes a function to run, given the user made a valid choice. If the choice is invalid an error message is shown. Whatever choices was made, after the action of that choice happens, the menu repeats, unless the menu option is to quit. Such kind of menus are displayed from the code under...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT