RANGE

i need assistance. please wat i need i my code if for when a number greater than 99 and less than 0 is inputted. it shhould give an error. and let me reenter the number again, same for the second and third number value. please assist. cheers



int value_1, value_2, value_3, average;

cout << " Enter three separate integers in the range of 0 to 99 \n\n";


cout << "Enter First number: ";
cin >> value_1; cout << "\n";


cout << "Enter Second number: ";
cin >> value_2; cout << "\n";

cout << "Enter Third number: ";
cin >> value_3; cout << "\n";
You could use a do while loop to loop if the user doesn't enter something valid

so:

1
2
3
4
do { 
cout << "enter first...";
// code
} while (value_1 is bigger than 99 and less than 0)
thanks i had done that, sorry i dint include this. what am actually looking for when a numebr lesser than 0 and greater than 99, it should output an error message till the correct value is entered for the three numbers. please would be very greatful if my code could be edited.. thanks





int _tmain(int argc, _TCHAR* argv[])

{
cout <<"\nweclome to c++\n";
cout <<"Enter three values in the range of 0 to 99.";

do
{

cout<<"\nvalue1: ";
cin >> nvalue1;

}
while (nvalue1 < 0 || nvalue1 > 99);


do
{


cout<<"\nvalue2: ";
cin>> nvalue2;

}
while (0 > nvalue2||nvalue2 > 99 );

do
{

cout<<"\nvalue3: ";
cin>> nvalue3 ;

}
while (0 > nvalue3||nvalue3 > 99 );
you can constuct a while loop like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int nvalue1 = -1;
while (nvalue1  == -1)
{
    cout << "nvalue1:\n";
    cin >> nvalue1;

    while (nvalue1 <0 || nvalue1 >99)
    {
        cout << "error\n";
        cout << "nvalue1:\n";
        cin >> nvalue1;

        if (nvalue1 > 0 && nvalue1 <99)
        break;
    }
}


this should definitely work

You can loop and if the value is OK break the loop otherwise show the error message and continue looping

eg:

1
2
3
4
5
6
7
8
9
int n;
while (true) // infinite loop
{
    cout << "Value? ";
    cin >> n;
    if ( n<99 && n>0 ) //input is OK
        break; // exit from the loop
    cout << "Invalid input!\n";
}
Last edited on
thank you guys alot. it work. very grateful
Topic archived. No new replies allowed.