Question #224891

Now, imagine your younger brother is working in a project with you. Both of you need to

solve negative number issue input by user. But you want to restrict information type

which can be send as error notification by your brother. So, write a program to handle the

scenario.


Expert's answer

This problem has several solutions first one is at the beginning of the program deeloper can ask the user not input the negative number:


int n;
cout << "Input any non-negative number: ";
cin >> n;


The second solution is developer can keep asking the user for a input until he/she inputs a valid(non-negative) number.

int n = -1;
cout << "Input any non-negative number: ";
while (n < 0) {
    cin >> n;  
}
LATEST TUTORIALS
APPROVED BY CLIENTS