Question

CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

CE – Return and Overload in C++

You are going to create a rudimentary calculator.

  1. The program should call a function to display a menu of three options:
    1. 1 – Integer Math
    2. 2 – Double Math
    3. 3 – Exit Program
  1. The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option selected.
  1. If the user selects 1 or 2, the program should allow the user to enter in any two numbers.
    1. The program must create 5 functions called add, subtract, multiply, divide and mod. These functions must take two integers as arguments and return an integer to the calling function. Each function performs the appropriate mathematical task.
    2. The program must create 4 functions called add, subtract, multiply and divide. These functions must take two doubles as arguments and return a double to the calling function. Each function performs the appropriate mathematical task.
    3. The program must create two functions called display.
      1. The first display function takes the five integer values calculated and displays them
      2. The second display function takes the four double values calculated and display them to two decimal places
  1. If the user select 3, the program should call a function to ask the user if they want to exit the program. The only valid responses are Y, y, N and n. The program must test that the user enters in a valid response. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return a Boolean back to the calling function telling the calling function to allow for a new entry to to end the program.
  1. The program should only end if the user selects 3 from the menu and y or Y that they want to end. There is no other way to end the program.
  1. Print some goodbye message.

  1. Notes:
    1. The program must declare all of the functions using prototypes.
    2. All functions except the display functions must return a value. The main program should always contain a variable to catch that return value.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


#include <iostream>

#include <string>

using namespace std;

// Function Declarations
int getValidMenuOption();
char getvalidExitOption();
int add(int a, int b);
int sub(int a, int b);
int divide(int a, int b);
int multiply(int a, int b);
int modulas(int a, int b);
void display(int ares, int sres, int mres, int dres, int modre);
double add(double n1, double n2);
double sub(double n1, double n2);
double divide(double n1, double n2);
double multiply(double n1, double n2);
void display(double ares, double sres, double mres, double dre);
int main() {
//Declaring variables
int choice, a, b;
double n1, n2;
char exitop;
  
/* This while loop continues to execute
* until the user enters a valid input
*/
while (true) {
   //displaying the menu
cout << "\n1 - Integer Math" << endl;
cout << "2 - Double Math" << endl;
cout << "3 - Exit" << endl;
cout << "Enter Choice :";
  
//getting the choice entered by the user
choice = getValidMenuOption();
  
//Based on the user choice the corresponding case will be executed
switch (choice) {
case 1:
{
cout << "Enter two numbers ( seperated by space ):";
cin >> a >> b;
int ares = add(a, b);
int sres = sub(a, b);
int dres = divide(a, b);
int mres = multiply(a, b);
int modres = modulas(a, b);
display(ares, sres, mres, dres, modres);
continue;
}
case 2:
{
cout << "Enter two numbers ( seperated by space ):";
cin >> n1 >> n2;
double ares = add(n1, n2);
double sres = sub(n1, n2);
double dres = divide(n1, n2);
double mres = multiply(n1, n2);
display(ares, sres, mres, dres);
continue;
}
case 3:
{
exitop = getvalidExitOption();
if (exitop == 'y' || exitop == 'Y') {
break;
} else {
continue;
}

}

}
break;
}

return 0;
}
int getValidMenuOption() {
int choice;
while (true) {
cin >> choice;
if (choice < 1 || choice > 3) {
cout << "** Invalid.Must be between 1 and 3 **" << endl;
} else {
break;
}
}
return choice;

}
char getvalidExitOption() {
char ch;
while (true) {
cout << "Do you want to exit the program (Y or y/N or n):";
cin >> ch;
if (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N') {
cout << "Invalid.Must be either 'Y' or 'y' / 'N' or 'n'" << endl;
} else {
break;
}
}

return ch;
}
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int divide(int a, int b) {
return a / b;
}
int multiply(int a, int b) {
return a * b;
}
int modulas(int a, int b) {
return a % b;
}
void display(int ares, int sres, int mres, int dres, int modres) {
cout << "Addition :" << ares << endl;
cout << "Subtraction :" << sres << endl;
cout << "Multiplication :" << mres << endl;
cout << "Division :" << dres << endl;
cout << "Modulas :" << modres << endl;
}
double add(double n1, double n2) {
return n1 + n2;
}
double sub(double n1, double n2) {
return n1 - n2;
}
double divide(double n1, double n2) {
return n1 / n2;
}
double multiply(double n1, double n2) {
return n1 * n2;
}
void display(double ares, double sres, double mres, double dres) {
cout << "Addition :" << ares << endl;
cout << "Subtraction :" << sres << endl;
cout << "Multiplication :" << mres << endl;
cout << "Division :" << dres << endl;
}

____________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...
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
  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • The language is C++ for visual studio express 2013 for windows create a TicketManager class and...

    The language is C++ for visual studio express 2013 for windows create a TicketManager class and a program that uses the class to sell tickets for a performance at a theater. Here are all the specs. This is an intense program, please follow all instructions carefully. -I am only creating the client program that uses the class and all it's functions -The client program is a menu-driven program that provides the user with box office options for a theater and...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Description: In the main program create a menu system that will allow a user to choose...

    Description: In the main program create a menu system that will allow a user to choose from 3 option. 1. Fraction calculator 2. Grading schema 3. Biggest integer Program #1 Fraction calculator: You will start by prompting the user for two fractions in the main. The inputs should be two strings in the form #/#. You must account for both positive and negative numerators. Therefor -#/# is also a valid input. In a separate function create a program that will...

  • Given the incomplete program below, write two functions, one called integer and another called decimal. You...

    Given the incomplete program below, write two functions, one called integer and another called decimal. You can use following incomplete program. Your assignment is to supply the necessary missing code: #include #include using namespace std; int integer (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER (A DOUBLE) //WRITE HERE THE CODE TO EXTRACT THE INTEGER AND RETURN IT } double decimal (….) { //REPLACE THE … WITH THE REQUIRED PARAMETER //WRITE HERE THE CODE TO EXTRACT THE DECIMAL PORTION,...

  • Create a new project called Date In This new project you are going to design a...

    Create a new project called Date In This new project you are going to design a class of Date. This mean that you need to create functions within the class to validate the Date for example: is it a leap year? How many days a months has? How many months are? Create a simple main menu capable of entering a date and validating a date. Instructions • Create a class of Date with attribues and member functions (remember the use...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

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
ADVERTISEMENT