Create a new file (in Dev C++) .In Lab 2, you created a menu for a simple calculator program.In Lab 6, you added some functionality based on the user selection.In Lab 7, you added some functionality for a main program loop and input validation.In this lab, you will create some general purpose functions to offload some of the tasks in main().Write functions for:displaying "hello" messagedisplaying menudisplaying "goodbye" messagedisplaying "invalid selection" message (for invalid menu selection)displaying "invalid input" message (for divide-by-zero input validation)Prototypes should be written for each functionFunction definitions can be in any order, after main()Call the functions as needed in main(), replacing the original codeThe program should:call function to display a "hello" message before presenting the menucall function to display the menuprompt user for selectionaccept either uppercase or lowercase for selectioncall function to display error message if invalid selection is made (example: E)display the selection back to the userprompt for two numbersperform the operation and display the resultsuse loop for input validation of second number during divisioncall function to display error message for division when second number is 0after displaying result, use loop to present menu again and ask for another selectiondo not display "hello" message againcall function to display a "goodbye" message before exiting the program

Question
Asked Jul 24, 2019
8 views

Create a new file (in Dev C++) .

In Lab 2, you created a menu for a simple calculator program.

In Lab 6, you added some functionality based on the user selection.

In Lab 7, you added some functionality for a main program loop and input validation.

In this lab, you will create some general purpose functions to offload some of the tasks in main().

Write functions for:

  • displaying "hello" message
  • displaying menu
  • displaying "goodbye" message
  • displaying "invalid selection" message (for invalid menu selection)
  • displaying "invalid input" message (for divide-by-zero input validation)

Prototypes should be written for each function

Function definitions can be in any order, after main()

Call the functions as needed in main(), replacing the original code

The program should:

  • call function to display a "hello" message before presenting the menu
  • call function to display the menu
  • prompt user for selection
    • accept either uppercase or lowercase for selection
    • call function to display error message if invalid selection is made (example: E)
  • display the selection back to the user
  • prompt for two numbers
  • perform the operation and display the results
    • use loop for input validation of second number during division
    • call function to display error message for division when second number is 0
  • after displaying result, use loop to present menu again and ask for another selection
    • do not display "hello" message again
  • call function to display a "goodbye" message before exiting the program
check_circle

Expert Answer

Step 1

Program Instructions:

  • Declare variables and call the printHelloMsg() function to display a welcome message.
  • Call printMenu() function to display the menu.
  • Always convert the user's choice to uppercase to allow the user to enter the choice in uppercase or lowercase.
  • Use a switch statement to run the selected operations along with the break statement to execute only one case at a time.
  • Print the user’s choice and the calculations.
  • Add input validation in the case D for divide by zero error and use function printInputValidation() to show the error message.
  • Use function printInvalidSelect() to display choice validation message in the default case of the switch statement.
  • Use a return statement to exit the program in case X and printGoodbyeMsg() to display the goodbye message.
Step 2

Program:

# include <iostream>

// to include toupper() function

#include <ctype.h>

using namespace std;

 

// prototypes

void printHelloMsg();

void printMenu();

void printInvalidSelect(char);

void printInputValidation();

void printGoodbyeMsg();

// main function

int main()

{

    char op,ch='a';

    float num1, num2;

    // function call for "hello" message.

    printHelloMsg();

    while(ch!='X'){

    //function call for the menu

    printMenu();

         //prompt user for selection

         cout<<"\nPlease enter you choice:";

         cin >> op;

         // for accepting either uppercase or lowercase for selection

         ch = toupper(op);

         switch(ch)

         {

             case 'A':

               //display the selection back to the user

               cout<<"You entered "<<op<<", add two numbers"<<endl;

               //prompt for two numbers

               cout<<"Please enter two numbers, separated by a space: ";

               cin>>num1>>num2;

               //perform the operation and display the results

                 cout << num1<<"+"<<num2<<"="<<num1+num2;

                 break;

                

             case 'B':

               cout<<"You entered "<<op<<", subtract two numbers"<<endl;

               cout<<"Please enter two numbers, separated by a space: ";

                 cin>>num1>>num2;

                 cout << num1<<"-"<<num2<<"="<<num1-num2;

                 break;

                

             case 'C':

               cout<<"You entered "<<op<<", multiply two numbers"<<endl;

               cout<<"Please enter two numbers, separated by a space: ";

                 cin>>num1>>num2;

                 cout << num1<<"*"<<num2<<"="<<num1*num2;

                 break;

    

             case 'D':

               cout<<"You entered "<<op<<", divide two numbers"<<endl;

               do{

                   cout<<"Please enter two numbers, separated by a space: ";

    &nb...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Give an implementation of the macroinstruction searchleft (a, qi, qj), which indicates that the mach...

A: Turing machine uses “searchleft” for an implementation of the macroinstructions. If the machine read...

question_answer

Q: I am trying to write a program to convert an integer to a hexadecimal. How do I do this without us...

A: As there is no particular programming language mentioned, we are doing this conversion of integer nu...

question_answer

Q: I am struggling on storing numbers from the getChoice () function into an array and pulling it back ...

A: Click to see the answer

question_answer

Q: This question is for Python. Thank you!

A: The algorithm of the file would be as below:Receive the inputs as a series of input commandsConcaten...

question_answer

Q: Question: Write a program that prompts the user to enter the number of students and each student's s...

A: Solution:The python program has the following algorithm:Prompt and read the total number of students...

question_answer

Q: Write a grammar for the language consisting of strings built only of theletters aand b. The strings ...

A: The given language L contains the strings that have infinite number of letters but the strings must ...

question_answer

Q: I am trying to write a program to convert a number from an integer to an octal. How do I do this wit...

A: As there is no particular programming language mentioned, we are doing this conversion of integer nu...

question_answer

Q: C++ my programming lab 11.7: Customer AccountsWrite a program that uses a structure to store the fol...

A: Solution:Given data,The C++ code use 20 structures to get the user data, change account information,...

question_answer

Q: Write a java program called Sales considering the following directions and the sample run. Practice ...

A: The program has the following steps:Declare the variablesReceive the input from the userCalculate th...