Ask Question
21 August, 16:34

Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter variable

+1
Answers (1)
  1. 21 August, 18:44
    0
    Following are the function

    void getNumber (int &x) / / function definition

    {

    int n; / / variable declaration

    cout<<" Enter number in the range of 1 through 100:";

    cin>>n;

    while (n100) / / iterating over the loop

    {

    cout<<"wrong input:"<
    cout<<" Enter number again in the range of 1 through 100:";

    cin>>n;

    }

    x=n; / / stored in the parameter variable

    }

    Explanation:

    Following are the code in c++

    #include / / header file

    using namespace std;

    void getNumber (int &x) / / function definition

    {

    int n; / / variable declaration

    cout<<" Enter number in the range of 1 through 100:";

    cin>>n;

    while (n100) / / iterating over the loop

    {

    cout<<"wrong input:"<
    cout<<" Enter number again in the range of 1 through 100:";

    cin>>n;

    }

    x=n; / / stored in the parameter variable

    }

    int main () / / main function

    {

    int x;

    getNumber (x); / / calling

    cout<
    return 0;

    }

    In this program we create a function getNumber as reference parameter of void type. Taking input by user in "n" variable and validate that the number in the range of 1 through 100 by using while loop if it is out of range then we again taking input by the user untill we will not provide the correct range.

    Output:

    Enter number in the range of 1 through 100:345

    "wrong input:

    Enter number again in the range of 1 through 100:45

    45
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers