Whitespace in Password Masking.

Newbie here. I cant seem to get the Space key working. The program still prints the space key as an asterisk. Here's my code:

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
40
41
42
43
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
main()
{
    char pass[10];
    char ast;
    int x=0;
    
    printf("\n\n\t\t\tEnter password:");
    while(ast!='\r')
    {
       ast=getch();
       if(ast==8 &&  x>0)
       {
          x--;       
          putch(ast);
          putch(' ');
          putch(ast);
       }
       else if(ast!=13 && ast!=8)
       {
          printf("*");
          pass[x]=ast;
          x++;
       }
       else if(ast==32)
       {
          printf(" ");
       }
       
    }pass[x]=0;
 
    system("cls");
    if(!strcmp(pass,"pass word"))
    {printf("\n\n\t\t\tWelcome!");}
    else
    {printf("\n\n\t\t\tINTRUDER ALERT!");}
    system("pause>nul");
    return 0; 
}


Any suggestion guys? Thanks.
Last edited on
Move lines 29..32 before line 23. Think about why this makes a difference. BTW, why the special handling for space characters? That is, why cannot a space be part of the password?
Hope this helps.
Sir Duoas. Thanks for the reply. It is greatly appreciated. I am still new to C++ and do lack skills in logic building. I am really having a hard time read the flow of a program. Especially how you manipulate the variables and constants. Also, in putting conditions to a loop or if statement. I tried the one you said. My guess is that so the program will try to check the two spaces before it displays the mask. Now, with your question Sir. Since I am still on the learning process. Can I ask what is special handling for space characters is? and the space cannot be a part of the password as my professor gave us the problem that it should not be masked by asterisk. Actually, it is a part of the password. My professor just don't want it to be masked. I already tried this though:

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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{ 
     char a[10],b;
     int c,d;

     printf("ENTER YOUR PASSWORD:");
     for(d=0;(b=getch())!=13 && d<9;d++)
     { 
        a[d]=b;
        if(b!=32)
        {
        printf("*");
        }
        else if(b==32) 
        {
        printf(" ");
        }
     }c=strcmp(a,"pass word");

     if(c==0)
     {
          system("cls");
          printf("PASSWORD ACCEPTED\n\t\t**WELCOME**");
     }
    else
    {
    system("cls");
    printf("WRONG PASSWORD\n\t\t**INTRUDER ALERT!!!**");
    }
 getch();
 return 0; 
}


And the problem with this one is the backspace key. What do you think about using escape sequences instead of ASCII codes? My classmates use it. I just don't want my professor to think that I just copied it from them. And I also don't what to develop the habit of copying other's work. I want to be better in my field of specialization. I'm going to need a lot of help and encouragement, I guess. And of course. Practice and Study. I really enjoy programming and It is my passion. I hope the people in this forum can help me. Thanks again Sir Duoas.
Using escape sequences is correct -- and they are part of the C standard -- so it is OK to use them.

Your first posting was closer to correct. The problem is that on lines 29-32 you don't add the key the user pressed (the space key) to the password. But on lines 23-28 you correctly add the key the user pressed to the password. You need to fix lines 29-32 to add the space key to the password also. Once done (and putting the lines 29-32 and lines 23-28 in the correct order like I first suggested) then your program should work just right.

BTW, you should get rid of those calls to system(). Your program should not be clearing the screen or PAUSE-ing.


One final note. I am not "Sir" Duoas -- in English that would mean that I were knighted, which I am not. http://en.wikipedia.org/wiki/Sir

Hope this helps.
Sorry for that Duoas. It is just a sign of respect. Never meant that I am referring to you as a knight. Would "Mister" be alright for you? I did what you have suggested. Thanks for that. Though, the space key still has a problem. It prints a space but also prints an asterisk. This is what I have done. Would you kindly check it for me? Thanks again. Midterms is coming. Sorry if I replied so late.


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
40
41
42
43
44
45
46
47
48
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

main()
{
    char pass[10];
    char ast;
    int x=0, y;
    
    printf("\n\n\t\t\tEnter password:");
    while(ast!=13)
    {
       ast=getch();
       if(ast==8 && x>0)
       {
          x--;       
          putch(ast);
          putch(' ');
          putch(ast);
       }
       if(ast==32)
       {
          printf(" ");
       }
       if(ast!=13 && ast!=8)
       {
          printf("*");
          pass[x]=ast;
          x++;
       }
     }pass[x]=0;
 
  
    if(!strcmp(pass,"pass word"))
    {
      printf("\n\n\t\t\tWelcome!");
           }
    else
    {
      printf("\n\n\t\t\tINTRUDER ALERT!");
           }
           
    getch();
    return 0; 
}


I have many questions in my mind. Maybe I would post them sometime. Thanks Duoas.
Topic archived. No new replies allowed.