problem with c program

Vinay Bharadhwaj

Estimable
Jul 17, 2014
2
0
4,510
Hey I have a problem with this program

#include <stdio.h>

int main( void )
{
char password;
password = NULL;
int n;
n = 1;
while (n != 6)
{
printf("Enter the password. \n");
scanf("%d", password);

if (password = "default")
{
printf("It was a correct password \n");
}
else
{
printf("Sorry wrong password \n");
n = n + 1;
}
return 0;
}
}

when I run it I get correct password for anything I type.
I would even appreciate if you could tell me how to convert this into a android app, I do have c4droid but I wanted to add things like factory reset if password is wrong for 6 times and it should quit the app as soon as the password is correct.
Any help would be appreciated. Thanks.
 
Solution
To discover the faults in your program read up on the difference between "=" and "==". Then read about strings to discover why you cannot compare strings with "==" (and certainly not with "=") and why declaring password as "char" is inappropriate. You'll also need to check out format strings for scanf (and printf) to decide whether "%d" is appropriate in this case.

I'd forget about Android until you have mastered the basics of C.

Ijack

Distinguished
To discover the faults in your program read up on the difference between "=" and "==". Then read about strings to discover why you cannot compare strings with "==" (and certainly not with "=") and why declaring password as "char" is inappropriate. You'll also need to check out format strings for scanf (and printf) to decide whether "%d" is appropriate in this case.

I'd forget about Android until you have mastered the basics of C.
 
Solution
You must first read a book or two on C Programming...
Even with changes iJack has proposed, your "program" will ask forever for password when correct one has been entered.

As for Android development - after mastering C, get a book on Java as well. Applications like one you've mentioned will not help you.