I decided to make one password program to show you how it looks. Since we are going to make a password program we will be using the string command because users are going to use either characters, numbers or symbols for their passwords. We will also use the string compare statement to compare the inputted password to the original password. Check out the sample codes below.
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char password [100];
int i;
for(i=0;i!=3;i++)
{
cout<<"\nENTER PASSWORD: ";
gets(password);
int ptr = strcmp ("reymond",password);
if(ptr==0)
{
cout<<"\nACCEPTED";
break;
}
else
{
cout<<"\nDENIED";
continue;
}
}
getch();
}
As seen above, the user is allowed only three times to enter his guess. In this matter, the for loop statement will be responsible in counting how many times the user had inputted a guess. If the user's input is evaluated the program will now jump in to the if statement. If the input satisfies the original password then the program will tell you that the password has been accepted and the program will terminate. On the other hand, if the input does not satisfy the original password the program will tell you that it is denied and will ask you two more times. Still if it does not satisfy, the program will automatically terminate. You can check out the sample output below.
Hope you learned something from this lesson. Feel free to leave a comment below. Thanks!
No comments:
Post a Comment
Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!