Problem with while loop: does not work or

2

The or does not work in this while loop:

#include <iostream>
using namespace std;
main ()
{
    char s;
    do{
        cout<<"\n\t\t> Ingrese el sexo: ";
        cin>>s;
    }while (s!='f' || s!='m');
    return 0;
}
    
asked by Malthael 23.08.2016 в 18:20
source

1 answer

5

It does not work for you because instead of || (OR) you should use & & amp; (AND). Using OR the condition of the while will always be true because when 'f' will not be 'm'; -)

#include <iostream>
using namespace std;
main ()
{
    char s;
    do{
            cout << "\n\t\t> Ingrese el sexo: ";
                cin >> s;
    }while (s!='f' && s!='m');
    return 0;
}
    
answered by 23.08.2016 / 19:47
source