Question

In: Computer Science

create a C++ program where you have 2 functions with two parameters. Limit the first function...

create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).

Solutions

Expert Solution

#include <iostream>
using namespace std;

//function1 that adds their parameters
int function1(int num1, int num2)
{
    return num1 + num2;
}


//function2 that adds their parameters
int function2(int num1, int num2)
{
    return num1 + num2;
}

int main()
{
    //for 4 numbers provided by user
    int num1, num2, num3, num4;
    //user choice for continue
    char choice;
    
    //if user selects y it will continue, or else at least 1 time execution
    do
    {
        //for number1
        do{
            cout << "Enter number 1 for function 1 : ";
            cin >> num1;
            if(num1 <= 1 || num1 >= 10)
            {
                cout << "Number must be from 1 to 10" << endl;
            }
        }while(num1 <= 1 || num1 >= 10);
        
        //for number2
        do{
            cout << "Enter number 2 for function 1 : ";
            cin >> num2;
            if(num2 <= 1 || num2 >= 10)
            {
                cout << "Number must be from 1 to 10" << endl;
            }
        }while(num2 <= 1 || num2 >= 10);
        
        //for number3
        do{
            cout << "Enter number 1 for function 2 : ";
            cin >> num3;
            if(num3 <= 5 || num3 >= 20)
            {
                cout << "Number must be from 5 to 20" << endl;
            }
        }while(num3 <= 5 || num3 >= 20);
        
        //for number4
        do{
            cout << "Enter number 2 for function 2 : ";
            cin >> num4;
            if(num4 <= 5 || num4 >= 20)
            {
                cout << "Number must be from 5 to 20" << endl;
            }
        }while(num4 <= 5 || num4 >= 20);
        
        //function call for adding first two numbers
        int result1 = function1(num1, num2); 
        
        //function call for adding third and fourth numbers
        int result2 = function1(num3, num4); 
        
        //adds a result of both
        int result = result1 + result2;
        
        //prints a result
        cout << "Result : " << result << endl;
        
        //takes user choice to continue
        cout << "Do you want to continue??(y/n) : ";
        cin >> choice;
        
    }while(choice == 'y');
    
    return 0;
}

Please refer below screenshot of code for indentation.

Sample output :


Related Solutions

Requirements: In this assignment, you are going to create two switch functions. The first function is...
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
How would I create a program in C++ where you build and maintain two binary search...
How would I create a program in C++ where you build and maintain two binary search trees of information? I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations: L -- for launching a satellite, which will save it's info to the first set or D -- for deorbit the satellite. The other operations I'm looking to implement are: F -- for find, where user...
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT