Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 11-01-2012, 05:38 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2012
Location: Ranchi
Posts: 1
Question C Programming help...


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!!!
BelalKhan is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 11-01-2012, 08:35 PM   #2 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: C Programming help...

You can match strings using strcmp().

Check strcmp - C++ Reference

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

Quote:
/* 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

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.
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:38 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2