0

When I run my program it asks for the password indefinitely. I enter the correct password but it simply loops back. There were no errors while I was compiling. To compile, I used:

gcc -ansi -W -Wall -pedantic -o prog myProgram.c
chmod +x prog

I am running ubuntu trusty. Here is the code related to the password:

char string[100] = "";

int main(void)
{
    char correctPassword[25] = "myPassword";
    char password[25] = "";

    system("clear");

    printf("Enter your password:\n");
    scanf("%s", password);

    if (password == correctPassword)
    {
        system("clear");
        printf("Enter a string:\n");
        scanf("%s", string);
    }
    else
    {
        system("clear");
        printf("Sorry, incorrect password\n");
        system("pause");
        main();
    }

updated code:

char string[100] = "";

int main(void)
{
    char correctPassword[25] = "myPassword";
    char password[25] = "";
    int ret;

    system("clear");

    printf("Enter your password:\n");
    scanf("%s", password);

    ret = strcmp(password, correctPassword);
    if (ret == 0)
    {
        system("clear");
        printf("Enter a string:\n");
        scanf("%s", string);
    }
    else
    {
        system("clear");
        printf("Sorry, incorrect password\n");
        system("pause");
        main();
    }
    return 0;
}

edit 2: i think this is beyond solved now, how do i mark it as such?

mee
  • 489
  • 5
  • 11
  • 19
  • 2
    Step through it with a debugger to see what's happening. You have almost all of the tools you need to figure this one out yourself. Had you done this, you might've asked a question like "Why does password == correctPassword always return false?" which is a *much better* question. It's also quite strange to be calling main recursively the way you do. – Pete Baughman Jun 05 '14 at 18:27
  • You cannot compare arrays using `==`. – Joel Cornett Jun 05 '14 at 18:28
  • i called main like that because i don't know any other way to go back to the beginning of main (to ask for the password again). if you could suggest something for that too i would be very grateful – mee Jun 05 '14 at 18:28
  • 1
    @mee: Use a while loop. Have you tried reading a book on C? That would be a great start. – Engineer2021 Jun 05 '14 at 18:29
  • @JoelCornett how else would i compare them to see if password is identical to correctPassword? – mee Jun 05 '14 at 18:29
  • @staticc ive started C by dennis ritchie, but i've only read about half of it – mee Jun 05 '14 at 18:30
  • @mee: Use a for loop and iterate through the array. There are newer books with better examples. I see no reason to start with Ritchie. – Engineer2021 Jun 05 '14 at 18:30
  • 1
    I think you decided to try the password program a bit too early. – cppcoder Jun 05 '14 at 18:33
  • yeah maybe, im just using it as an excuse to not study :) – mee Jun 05 '14 at 18:46
  • 2
    BTW... Don't call main() in your failed password attempt. That causes a lot of stuff to pushed onto the stack, and if the CORRECT password is entered, and program control gets passed off somewhere else, when the code exits, it has to unroll all that. The way you've authored the code above would call for the use of a label, and a GOTO. However, if you ask 10 different C programmers how to do this, you're likely to get 10 different answers. Everything from a for() loop, so you exit after x attempts, to a while loop, to a goto, to a do loop... You GOTTA love the C language! – LarryF Jun 05 '14 at 18:51
  • ok, gonna google gotos and labels – mee Jun 05 '14 at 19:16

2 Answers2

3

This line

if (password == correctPassword)

does not do what you think it does. It isn't comparing the strings, it is comparing the memory address of the first character of each string. If you want to compare the strings, you want to use strcmp, which you can read about here: http://www.tutorialspoint.com/ansi_c/c_strcmp.htm

EDIT In response to the change in the code; you have the line

if(ret = 0)

You want

if(ret == 0)

I'm assuming this is a typo.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • i updated my code to use strcmp(), but now I get 2 warnings. where should I paste my updated code for you to see? – mee Jun 05 '14 at 18:39
  • Go ahead and edit your question; I'll edit my answer accordingly – wolfPack88 Jun 05 '14 at 18:40
  • This is the 100% correct answer... If you get warnings, what are they? Remember, strcmp() under some CRT's has changed to strcmp_s(), which allows you to set an absolute MAX on the LENGTH of the strings preventing the possible case of say dumping in an entire FILE into your small Password field and overflowing the stack, resulting in an exploit in your program. – LarryF Jun 05 '14 at 18:43
  • HA! Thats right :) its a type. ok hold on ill update and check again. missed that one – mee Jun 05 '14 at 18:44
  • ok, now the first if block works. If i enter the correct password it asks for a string. but if i enter a wrong password it just loops again instead of informing me that ive entered a wrong pass, then going back – mee Jun 05 '14 at 18:48
  • Try adding the line `fflush(stdout);` after the `printf` statement. The reason you need that is because of I/O buffering: http://stackoverflow.com/questions/1450551/buffered-i-o-vs-unbuffered-io – wolfPack88 Jun 05 '14 at 18:51
  • i added it but it still reacts in the same way – mee Jun 05 '14 at 19:00
-3

Add an ampersand in the scanf statement.

scanf("%s", &password);
evan
  • 1
  • 1