C Programming help...

BelalKhan

Right off the assembly line
hey guyz!!!
i have to write a c program
in which
a password is set initially by us
and the user will have 3 chances to enter the password
if the user enters correct password
output will be "You Have logged in correctly"
and if he entered wrong password
output will be "Wrong password try again"


any helps!!! please!!! :oops: :oops: :-o :razz:
 

krishnandu.sarkar

Simply a DIGITian
Staff member
You can match strings using strcmp().

Check strcmp - C++ Reference

So the example there does the exact thing you are looking for...

/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}

What it does is...

1. Assigning default value to a char array. You can consider char pointer too, in that case you can use malloc() and strcpy(var, value); to set default password.

2. It takes input from the user.

3. And keeps matching until it's matched.

So in your case you can consider taking a variable i and run it for 3 times. In that case go for while loop instead of do...while.

And also you'd need a if...else to print Wrong Answer and Correct Answer depending on matching. And break once it gets matched.

So now try it out yourself and if it doesn't works, post the program here. We'll help you out.

But don't ask us to write your programs, as no one would do so. Not because we can't but because we want you to learn it yourself :p

Best of Luck.

UPDATE : Don't use gets(), it's dangerous.

Check Things to Avoid in C/C++ -- gets() , Part 1 - GIDNetwork

So it's better not to practice wrong things.

Thanks to LFC_Fan for correcting me. :)
 
Top Bottom