0

any idea how to create a loop if user input wrong characters..?? i tried to do while loop but it never works.. anyone..??

#include <iostream>
using namespace std;

int main () {
double input_x, number;
cout << "Insert value: ";
cin >> input_x;


char choice ; 
choice = 'D', 'R', 'd', 'r';

cout << "Insert choice: ";
cin >> choice;

while ( //what suppose i write here?? )

if ( choice == 'D' || choice == 'd' ) {
number = input_x * 3.142 / 180;



}	
else if ( choice == 'R' || choice == 'r') {
number = input_x;

}
else {
cout << "Invalid input!" ;
}

}
cout << "The number in radian is: " << number << endl;
}

Edited by Nick Evan: Added [CODE] tags

3
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by r.evrard
0
#include <iostream>
using namespace std;

int main ()
{
    double input_x, number;
    cout << "Insert value: ";
    cin >> input_x;
    char choice;
    bool ok=false;
    do
    {
        cout << "Insert choice (D or R): ";
        cin >> choice;
        switch (choice)
        {
        case 'D':
        case 'd':
            number = input_x * 3.142 / 180;
            ok=true;
            cout << "The number in radian is: " << number << endl;
            break;
        case 'R':
        case 'r':
            number = input_x;
            ok=true;
            cout << "The number in radian is: " << number << endl;
            break;
        default:
            cout << "Invalid input. Please choose again.\n";
        }
    }
    while(ok==false);
}
0
/*There are several possibilities. Here is one.*/
#include <iostream>
using namespace std;

int main () {
double input_x, number;
cout << "Insert value: ";
cin >> input_x;


char choice ; 
//choice = 'D', 'R', 'd', 'r';          I don't understand the purpose of this instruction

choice = 'e';       //Initialize choice so that it be incorrect

while ( choice != 'D' && choice != 'R' && choice != 'd' && choice != 'r' ) {
            //while no correct choice has been made
    /********** The while loop must start here so that, in case of error,
                                a second choice can be made **************/

cout << "Insert choice: ";
cin >> choice;

//while ( //what suppose i write here?? )

if ( choice == 'D' || choice == 'd' ) {
number = input_x * 3.142 / 180;

}   
else if ( choice == 'R' || choice == 'r') {
number = input_x;

}
else {
cout << "Invalid input!" ;
}

}
cout << "The number in radian is: " << number << endl;

}

Edited by Nick Evan: Fixed formatting

0

Your while loop is not ok, its infinite. How could be answer == D && for example R?

I meant that : while ( choice != 'D' && choice != 'R' && choice != 'd' && choice != 'r' )

I didn't really read it through carefully, so there can be other problems as well.

Edited by blackrainbowhun: n/a

0

A do while loop is more appropriate to your problem than a while loop, since then the condition is checked at the end of the loop. Of course, this is a detail.

0

A and condition returns false when any one of the propositions is false. Therefore, if choice == d for instance, the proposition choice != d is false as well as the whole condition and the while loop will not be repeated. Of course, the solution using an error flag is easier to understand. Maybe it's the one I would use myself, but I tried to stay close to your version of the program. Of course I checked the solution I sent you and it seems to work.

0

while ( choice != 'D' && choice != 'R' && choice != 'd' && choice != 'r' )

while its not D AND not R...its OR you meant? :)

0

You can also use

while ( !(choice == 'D' || choice == 'R' || choice == 'd' || choice == 'r') )

Again I agree that blackrainbowhun's version with an error flag is easier to read.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.