Thread: help

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    help

    the program asks the id and password of the user, then compares it to the list of IDs and Passwords in a ready file..i think theres something wrong with the while loop.when i run the program, it stops working after i enter the id and password.it doesnt enter the while loop.
    Code:
                printf("\nKULLANICI ISIM: ");
                scanf("%s",&tempid);
                printf("\nKULLANICI SIFRE");
                scanf("%s",&temppass);
                while((c=getc(fidpasstud))==tempid&&(d=getc(fidpasstud))==temppass)
                {......

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I assume tempid and temppass are of type char. %s, however, reads arrays of characters, not single characters. If the variables are of type char, your code trashes random areas of memory in the scanf() calls.

    If you are intending tempid and temppass to be strings, you need to compare characters read (using getc()) with elements of the string ... individually.

    Beyond that, your code is so unspecific, it is not possible to help you.

    Try creating a small but complete sample of code that exhibits your problem. By "complete" I mean one that you can compile and link, and it exhibits your problem. Then folks who are not mind readers (which is the majority here, AFAIK) might have a chance of understanding your actual problem and helping you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    same problem

    I did what you said, and the problem is the same.The program ends without showing the phrase "Correct id and password".I already have a file containing Ids and passwords.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    FILE *filename;
    int main()
    {
             char tempid[10],tempass[10],c,d;
             printf("enter ID:");
             scanf("%s",&tempid);
             printf("enter PASS:");
             scanf("%s",tempass);
             filename=fopen("ids.txt","r");
             while((c=getc(filename))==tempid&&(d=getc(filename))==tempass)
             {
                        printf("Correct ID and password.");
             }
             fclose(filename);
             return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have some issues there.

    &tempid should be just tempid in scanf.

    Then in your while loop you are reading in characters and compare them each to memory addresses of tempid and tempass. If you do like this you need to dereference both tempid and tempass and update them in each iteration or you will compare to the same character every time. The easiest way to do this is to use an array index that you update.

    Edit:

    That will still not work actually, it requires the strings to be interleaved in the file since you read one character for the id followed by one character for the pass. There are other issues with this as well, if your password and username are not the same length it wont work.
    Last edited by Subsonics; 01-24-2012 at 06:05 AM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    i changed &tempid, but still nothing...

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Of course. What about the other two paragraphs? You need to change you approach IMO. If the username is "user" and your password is "pass" you need to store them like this with your current method: "upsaesrs"

    After you compare characters, which means dereferencing tempid and tempass.

    It would be better to do this separately instead and compare the entire string with strcmp.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    i'm pretty new to C so dont get mad (u can laugh if you want)...but what you just typed sounds like giberish..i'm sorry..can u use smaller words?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Getting at the characters requires something like this: tempid[0], tempid[1] and so on to get the first and second character. But you read one character and expect that to belong to username and the second character to belong to password.

    Read password first, then username. Not both at the same time if you want to use your current method.

    The best is probably to read the password and user name separately. Then compare them in a separate step, you can use strcmp() for that, it takes two strings as argument and returns 0 if they are the same. Beware of the newline character ('\n') however which might be present in your file.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    ok.i thought meant that the first character of the username is tempid[0], second character is tempid[2] and so on.

    in the file, there's going to be many IDs and Passwords.What i was thinking was that getc returns one character at a time and it will compare that character with the characters if the username..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    FILE *filename;
    int main()
    {
        char tempid[10],tempass[10],c[10];
        printf("enter ID:");
        scanf("%s",tempid);
        printf("enter PASS:");
        scanf("%s",tempass);
        filename=fopen("ids.txt","r");
        while(getc(filename)!='\n')
        {
            c=getc(filename);
            if(strcmp(c,tempid)==0)
            printf("Correct ID and password.");
        }
        fclose(filename);
        return 0;
    }
    something like this?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try comparing the result of getc() with tempid[i], where i is an integral value that starts at zero and is incremented each time around the loop.

    Don't ignore the fact that, if you call getc() twice without storing the result anywhere (for example, to a variable), you lose the first character.

    And, please, filename is a rotten name for a FILE pointer. fopen() accepts a string that representing the filename. What it returns is not a filename.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed