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;
}
#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);
}
/*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;

}

thanx for the help guys.. ^^,

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.

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.

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.

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.