Ask Question
8 July, 09:54

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

+3
Answers (1)
  1. 8 July, 12:33
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers