What wrong in this program??

This is a simple program of a password matching..
I am not gettingthe desired output of this program...
Please help me out...


  • [color=Blue]strcmp() is comparing until ZERO terminator is found and in char array [b]a[/b] - there is no zero. Two choices here:

    1. Initialize [b]a[/b] with all zeroes right away:[/color]
    [code]char a[20] = {0};[/code]
    2. When ENTER pressed - code 13 is received and you break from the loop. Before calling strcmp() - you need to store zero at the end of entered array.
  • Can you please correct this program and post it here.....
  • [color=Blue]Use code line I posted in my #1 suggestion.[/color]
  • always keep in mind the good habit of initializing variables after declaring them

    good luck coding
  • always keep in mind the good habit of initializing variables after declaring them

    good luck coding
  • several things are wrong.

    you have a variable "int b" that is never referenced so this program shouldn't compile.

    the for loop is incremented one to many times,it should be c[19]. This doesn't matter so much because it breaks at the 14th position anyway.

    the last call to getch doesn't loop. meaning it gets one key press and the program exits.

    and the characters entered into the array c do not have a null terminator

    my advice is to remove the array c[] and rewrite it as a char *, assigning a string from input to it so that when you press enter the null terminator is automatically placed. Remove the variable b and the last call to getch() as they are both irrelevant also remove the conio.h and iostream.h headers and use stdio.h; I don't know if this will remove the functionality of clrscr() but it seems right.
  • Actually, When u Press Enter so it stores in char array in last.
    thats why your password string is not matched.
    I Correct your Program, Now it will work as your requirement.....

    void main()
    int a,b;
    char c[20];
    Enter password:-
    c[a] = '';

    if(!strcmp(c,"password")) cout << "Password accepted.
    else cout << "Access denied.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!