Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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...

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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]
    [color=Blue]
    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.
    [/color]
  • Anirban0MitraAnirban0Mitra Posts: 2Member
    Can you please correct this program and post it here.....
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Use code line I posted in my #1 suggestion.[/color]
  • quakeboxquakebox Posts: 2Member
    always keep in mind the good habit of initializing variables after declaring them

    good luck coding
  • quakeboxquakebox Posts: 2Member
    always keep in mind the good habit of initializing variables after declaring them

    good luck coding
  • raiffaraiffa Posts: 4Member
    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.
  • MuhammadMubeenMuhammadMubeen Posts: 8Member
    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.....

    #include
    #include
    #include
    void main()
    {
    int a,b;
    char c[20];
    clrscr();
    cout<<"
    Enter password:-
    ";
    for(a=0;a<20;a++)
    {
    c[a]=getch();
    if(c[a]==13)
    {
    c[a] = '';
    break;
    }
    cout<<"*";

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

Sign In or Register to comment.