Thread: password

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    password

    I have a simple C program which basically compares two strings and reports whether they are the same or not. Hence it can be used to verify whehter 2 given passwords are the same or not.

    I wanted to know how you can make the '*' sysmbol appear instead of the password characters.

    example:

    if input is "admin"

    Enter password : ****

    Re-enter password : ****


    I was doing a for loop for that, but cannot get it to work.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #define ff fflush(stdin);
    
    void main()
    {
    int r;
    char str1[10], str2[10];
    clrscr();
    
    printf("Enter password:\n");
    gets(str1);
    ff;
    printf("Re-enter password:\n");
    gets (str2);
    ff;
    
    r=strcmp(str1, str2);
    
    if(r==0)
    {
    printf("Enter");
    }
    else
    {
    printf("Exit");
    }
    getch();
    }
    i made a this small example with for loop but cannot connect it with the gets() function

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char name[6] = "amar";
    	int i;
    
    	for(i = 0; i < strlen(name); i++) {
    		printf("*");
    	}
    
    	printf("\n");
    }

    If you could help me there ..
    thanks anyways.....

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I don't think 'getch' is portable, but you should be able to use it for this (I notive you've used it to await a key press at the end). You'd read the password one character at a time in a loop and have the loop print a '*' every time a key is pressed. Btw, your second program used int main, which is correct, void main isn't.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh boy, the poisonous trio of "void main", "fflush(stdin)" and "gets()" all in the same program!!!

    Read the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    ok i know this has been long... but i didn't want to continue on the code... but then i thought about it...and i got this

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
    int i=0, ch=0;
    char str1[10];
    clrscr();
    
    printf("Enter password:\n");
    for (i=0; i<80; i++)
    {
    ch = getch ();
    if (ch == '\r' || ch == '\n') break;
    if ((ch == '\b') && (i > 0))
    {
    printf ("\b%c\b", ' ');
    i -= 2;
    }
    else
    {
    putc('*', stdout);
    
    }
    }
    }
    the code is a bit disoragnized ...but it now prints "*" for and input of a char

    so it works...
    but i cannot get it to compare the string input and the password string ??
    how do i do this...

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
    int i=0, ch=0;
    char str1[10], passw[10] = {0};
    clrscr();
    
    printf("Enter password:\n");
    for (i=0; i<80; i++)
    {
    ch = getch ();
    
    passw[i] = ch;// now you can compare this with your string
    if (ch == '\r' || ch == '\n') break;
    if ((ch == '\b') && (i > 0))
    {
    printf ("\b%c\b", ' ');
    i -= 2;
    }
    else
    {
    putc('*', stdout);
    
    }
    }
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    thx i give it a go!!!

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    the code is a bit disoragnized ...but it now prints "*" for and input of a char

    so it works...
    but i cannot get it to compare the string input and the password string ??
    how do i do this...
    Read string1, read pw str, and then try

    if((strcmp(pw_str, str_input)) == 0)
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    thx for helping ronin,

    but GaPe's post gave away the whole answer....

    i finished it ..

    thx for all ......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM