Thread: How to break first character is wrong in password program?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Question How to break first character is wrong in password program?

    I means Password is ev1l.if keyboard input is a,input will stop and go work for next line and then input again.How to break it first char is wrong in password program?
    My Program code is here.,
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int main ()
    {
    
        char pass[]="ev1l";
        char password[10];
        int loginAttempt = 0;
    
        while (loginAttempt < 5)
       {
           printf("Enter Password: ");
          scanf("%s",password);
          if(strcmp(password,pass)==0)
            {
                    printf("Correct! Congratulations!\n");
                break;
            }
    
            else
            {
                    printf("Password is wrong!.\n");
                    loginAttempt++;
            }
        }
        if (loginAttempt == 5)
        {
                printf("Only five attempts tried. Program will exit.");
             return 0;
        }
    getch();
    return 0;
    }


    Please help me ,I'm newbie in C programming.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you want to compare-as-you-type, then you need to do your own string input based around getch().

    scanf takes (by default) line buffered standard input, so you only get to see what the user typed when they press enter.
    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.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Compare-as-you-type may be less secure. It let's hackers know where their guess is wrong, which is more information than simply saying "Password is wrong!"

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    not clear bro, can u show me sample bro :-( coz,i was new in C programming

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by ev1l View Post
    not clear bro, can u show me sample bro :-( coz,i was new in C programming
    which bro are you talking to, bro? one bro said to use getch. the other bro said that comparing a char at a time is stupid for a password cuz it tells a bro which char the bro got wrong making it totally insecure, bro

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by ev1l View Post
    not clear bro, can u show me sample bro :-( coz,i was new in C programming
    Visit the FAQ. Hint: Look for an entry with the word "enter".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I cant tell what is wrong with my break statements
    By blackfox_1109 in forum C Programming
    Replies: 3
    Last Post: 11-08-2013, 07:58 PM
  2. program break help!!
    By amir10z in forum C Programming
    Replies: 1
    Last Post: 01-02-2011, 10:10 AM
  3. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  4. Character Tables ( Password Encryption )
    By Necrofear in forum C++ Programming
    Replies: 26
    Last Post: 11-14-2006, 12:54 PM
  5. Break Character
    By Thantos in forum C Programming
    Replies: 4
    Last Post: 10-02-2001, 07:35 PM

Tags for this Thread