In: Computer Science
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).
#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 :