193k views
2 votes
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 Answer

3 votes

Answer:

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(n<1 || n>100) // iterating over the loop

{

cout<<"wrong input:"<<endl;

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

cin>>n;

}

x=n; // stored in the parameter variable

}

Step-by-step explanation:

Following are the code in c++

#include <bits/stdc++.h> // 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(n<1 || n>100) // iterating over the loop

{

cout<<"wrong input:"<<endl;

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<<x;

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

User ChillyPenguin
by
5.0k points