comparing chars

does anyone know why this code wont work? the pass word is 'mama' and it is not case sensitive, its purpose is to say invalid as soon as one wrong character is inputted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main(){
    char pass, pass2, pass3, pass4;
    bool valid;
    
    do{
       valid=true;
       cout<<"                           Enter Password:";
       scanf("%c",&pass);
       fflush(stdin);
       if(pass != 'm' || pass != 'M'){
           cout<<"Invalid password, please try again.";
           valid=false;
       }
       scanf("%c",&pass);
       fflush(stdin);
       if(pass != 'a' || pass != 'A'){
           cout<<"Invalid password, please try again.";
           valid=false;
       }
       scanf("%c",&pass);
       fflush(stdin);
       if(pass != 'm' || pass != 'M'){
           cout<<"Invalid password, please try again.";
           valid=false;
       }
       scanf("%c",&pass);
       fflush(stdin);
       if(pass != 'a' || pass != 'A'){
           cout<<"Invalid password, please try again.";
           valid=false;
       }       
    }while(!valid);
    cout<<"success";
    system("pause");
}
fflush is the reason it doesn't work.
This is what I have now, for some reason it still doesnt work properly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <conio.h>
using namespace std;

int main(){
    char pass;
    bool valid;
    
    do{
       valid=false;
       cout<<"\nEnter Password: \n--> ";
       pass=getche();
       if(pass == 'm' || pass == 'M'){
           valid=true;
       }
       valid=false;
       pass=getche();
       if(pass == 'a' || pass == 'A'){
           valid=true;
       }
       pass=getche();
       fflush(stdin);
       if(pass == 'm' || pass == 'M'){
           valid=true;
       }
       pass=getche();
       fflush(stdin);
       if(pass == 'a' || pass == 'A'){
           valid=true;
       }
    }while(!valid);
    
    cout<<"success";
    system("pause");
}
Your original if statements were faulty in that they were always true. You said to report the character as invalid is if it was not 'm' or not 'M'. Since it can't be both, it has to be not one or the other, so it is always invalid. In your new code, you're going to be resetting the validity every time you input a character, which is probably not what you want.
Well, now your 'valid' logic is wrong and you're using a non-standard unbuffered console input function and mixing it with fflushing stdin. I'd say you've gone downhill considerably since your first post.
Last edited on
I got it to work now, it just isn't viewer-friendly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
do{
       valid=false;
       cout<<"\nEnter Password: \n--> ";
       pass=getche();
       if(pass == 'm' || pass == 'M'){
           pass=getche();
           if(pass == 'a' || pass == 'A'){
               pass=getche();
               if(pass == 'm' || pass == 'M'){
                   pass=getche();
                   if(pass == 'a' || pass == 'A'){
                       pass=getche();
                       valid=true;
                   }else cout<<"\nInvalid password, please try again.\n";
               }else cout<<"\nInvalid password, please try again.\n";
           }else cout<<"\nInvalid password, please try again.\n";
       }else cout<<"\nInvalid password, please try again.\n";
    }while(!valid);
}
If you are familiar with C-style strings:

1
2
3
4
5
6
7
8
9
10
11
const char password[] = "MAMA" ;
enum { N = sizeof(password) - 1 } ; // number of characters in password

for( int i = 0 ; i < N ; ++i )
{
    if( std::toupper( getche() ) != password[i] )
    {
        std::cerr << "\nInvalid password, please try again.\n" ;
        i = -1 ; // repeat the for loop all over again
    }
}
Topic archived. No new replies allowed.