In: Computer Science
Overload and test function getNum(int &) to work with a parameter of type double.
2. Overload and test function doubleNum(int) to also work with a parameter of type double.
3. Both functions for collecting input should validate such that negative values would not be allowed.
Perform the following steps:
a. Add the function prototypes at the top.
b. In main(), add a new variable of type double. You can name it "value2" to distinguish from the other variable.
c. Write a new getNum() function with a parameter of type double. A copy/paste should be fine with the corresponding modifications.
d. Add to both getNum() functions input validation such that negative values are not allowed.
e. Write a new doubleNum() function with a parameter of type double. Again copy/paste is fine but with corresponding modifications.
f. In main() add code for working with the variable of type double that resembles the same logic (statements) for the integer variable.
// This program uses 2 functions: a void function with a reference
// variable as a parameter, and a value-returning function.
#include <iostream>
using namespace std;
// Function prototypes
void getNum(int &);
int doubleNum(int);
int main()
{
int value;
// Call getNum to get a number and store it in value
getNum(value);
// Call doubleNum, passing it the number stored in value
// Assign value the number returned by the function
value = doubleNum(value);
// Display the resulting number
cout << "That value doubled is " << value << endl;
return 0;
}
/**********************************************************
* getNum *
* This function stores user input data in main's value *
* variable by using a reference variable as a parameter. *
*********************************************************/
void getNum(int &userNum)
{
cout << "Enter a number: ";
cin >> userNum;
}
/***********************************************************
* doubleNum *
* This function doubles the number it receives as an *
* argument and returns it to main thru a return statement.*
***********************************************************/
int doubleNum (int number)
{
return number * 2;
}
Code for your program is provided below. The code is explained in code comments. Screenshot of output as well as code in IDE is provided in the last for your better understanding. If you need any further clarification please feel free to ask in comments.
#################################################################################
CODE
#include <iostream>
using namespace std;
//function prototype for integer value
void getNum(int &);
int doubleNum(int);
//overloaded function prototype for double value
void getNum(double &);
double doubleNum(double);
int main()
{
int value; //integer variable
getNum(value); //take input
value = doubleNum(value); //call double to double it
cout << "That value doubled is " << value << endl; //display double number
cout<<"\n";
double value2; //create double variable
getNum(value2); //take input
value2 = doubleNum(value2); //double the number
cout << "That value doubled is " << value2 << endl; //display it
return 0;
}
//take input for integer
void getNum(int &userNum)
{
do //loop
{
cout << "Enter a number: ";
cin >> userNum;
}while(userNum<0); //loop if number is negative
}
int doubleNum (int number)
{
return number * 2; //return double of the number
}
//overloaded function to take double input
void getNum(double &userNum)
{
do
{
cout << "Enter a number: ";
cin >> userNum;
}while(userNum<0); //loop if number is negative
}
//overloaded function to return two times of a double number
double doubleNum (double number)
{
return number * 2.0;
}
##########################################################################
OUTPUT
###############################################################################
CODE IN IDE