Thread: char swaps, and echo() password masking

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    char swaps, and echo() password masking

    I am currently having two problems with my program for school. It's a database for usernames and passwords

    1) I have to read from a file 2nd dimension of arrays (passwords) and change the password in the output file with numbers staying the same and letters A going to z B going y Z going to a etc. i know the offset is 32 and A starts with 65 but i cant think of how to write the code


    2) Our professor wants the input of the password to be masked with "*" but their is a catch. It must compile in GCC so conio.h is out of the question. I tried to do the following but it just runs without inputting the mask.

    Code:
    void usernamepsw(){//scanning in user info
    	
    	printf("Enter Username:");
    	
    	for (k=0; (c = getchar()) != '\n'; ++k){//stores the user name as "user".
    		user[k] = c;
    		}
    	user[k] = '\0';
    		
    	if (strcmp(user, "quit") == 0){
    	printf("Thank You for using the Database!\n");	
    	
    	fclose(fp);
    	fclose(fileout);
    
    
    	exit(0);
    	}
    
       cbreak();     //To disable the buffering of typed characters and get a character-at-a-time input, you need to call this.
       noecho();     //To suppress the automatic echoing of typed characters, you need to call this.
    	printw("\nEnter Password:");
    	
    	for (k=0; (c = getch()) != '\n'; ++k)
    	printw("*");  //Print a '*' instead of the actual charater read.{//stores the password as "psw".
    		psw[k] = c;
    		}
    	psw[k] = '\0';
    	
    	}
    Also thats just the function, before the function was called in main i used
    Code:
    initscr();    //Before you use any other curses routines, the initscr() routine must be called first.
       nocbreak();   //Normally we would read buffered input
       echo();       //Normally we would like to have the echo of what we read on the screen
    and at the end i used the terminating functions too

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but it just runs without inputting the mask.
    Code:
    >	for (k=0; (c = getch()) != '\n'; ++k)
    >	printw("*");  //Print a '*' instead of the actual charater read.{//stores the password as "psw".
    >		psw[k] = c;
    >		}
    You're missing a beginning brace on the for-loop, that might be the problem.

    To do the conversion, you could use a formula like so:
    Code:
       if (isupper(password[k]))
          out[k] = 'a' + (25 - (password[k] - 'A'));
       else if (islower(password[k]))
          out[k] = 'A' + (25 - (password[k] - 'a'));

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    but it just runs without inputting the mask.
    That won't run for me. You never define c, k, fp, fileout, psw, or user. I can guess at their types, but where is c & k & fp? I sincerely hope they're not global variables - they shouldn't be.

    If I put in the { as suggested above, it will output the *s.
    Why do you mix calls to getchar() and getch() ? Choose one, an stick to it. It seems to me that code runs much better with getch() only. You seem to be using curses, and I believe that printf()s won't show up (they don't for me) in curses. Use the curses equivalent. You may also need to call refresh() to see any updates.

    I tested with:
    Code:
    int main()
    {
    	initscr();
    
    	usernamepsw();
    
    	endwin();
    	return 0;
    }
    For me, the user name is not echoed. (man pages seems to say otherwise...) If you call this function more than once, ensure that either echoing is on, or Do It Yourself.
    (I personally perfer the unix style of not echoing anything, even *s at password prompts, but if your instructor says use *s, then that's his call. Really, either way is fine.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed