Reputation: 29
As you can see from the title I want to check password but with do while loop. What I want to do is, asking the user to enter the password and if the password is incorrectly entered 3 times, the program should exit.
Here is the code, I hope you understand what I want to do
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n = 0;
char s[10] = {'s','a','m','e','d'};
char unos[10];
int i;
do
{
for (i = 0; i < 5;i++) {
unos[i] = _getch();
_putch('*');
}
cout << endl;
for (i = 0; i < 5; i++)
{
if (unos[i] == s[i])
{
cout << "Your password is correct" << endl;
break;
}
else if (unos[i] != s[i])
{
do
{
cout << "Your password is incorrect" << endl;
cout << "Enter again: ";
for (i = 0; i < 5;i++) {
unos[i] = _getch();
_putch('*');
}
n++; // how many times user entered the password
}while(unos[i] != s[i]);
}
}
}while(unos[i] != s[i] && n < 3);
return 0;
}
The console output is correct, or it does what I want if I enter the correct password for the first time but if I made a mistake it doesnt do anything after that or actually it does ask me again to enter the password but it doesnt, show the message Your password is correct
.
If you now how to do this task even with recursion it would help me a lot.
Thanks in advance :)
Upvotes: 0
Views: 1238
Reputation: 316
Browsing Internet, I read http://www.cplusplus.com/articles/E6vU7k9E/ . Why not use the getch and getpass functions described there, with the main suggested, easier to read ?
int main()
{
string s = {'s','a','m','e','d'};
string unos;
int ntry (0);
bool ok (false);
while (!ok && (ntry < 3)) {
unos = getpass("Please enter the password: ",true);
if(unos==s) {
cout <<"Correct password"<<endl;
ok = true;
}
else {
if (ntry < 2) {
cout <<"Incorrect password. Try again"<<endl;
}
else {
cout << "access denied" << endl;
}
++ntry;
}
}
return 0;
}
Upvotes: 0
Reputation: 146
Following loop will never end:
do
{
cout << "Your password is incorrect" << endl;
cout << "Enter again: ";
for (i = 0; i < 5;i++) {
unos[i] = _getch();
_putch('*');
}
n++; // how many times user entered the password
}while(unos[i] != s[i]);
1) n < 3
is done out of this loop, so number of times is not considered
2) at the moment of }while(unos[i] != s[i]);
i = 5 (you declared i earlier), so you are comparing unos[5]
and s[5]
. These values are not initialized.
There are other issues in this code:
If the mentioned loop would end it would exit to for (i = 0; i < 5; i++)
, it also has no check of n
.
The main issue in my opinion is that you are checking only the first letter of entered password, so entering "sqwer" would give you "Your password is correct".
This code should be completely reworked. Working example (if continuing your idea) would look like this:
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
int n = 0;
char s[10] = { 's', 'a', 'm', 'e', 'd' };
char unos[10];
bool is_pwd_correct = false;
do {
for (int i = 0; i < 5; i++) {
unos[i] = _getch();
_putch('*');
}
for (int i = 0; i < 5; i++) {
if (unos[i] != s[i]) {
n++;
cout << endl << "Your password is incorrect" << endl;
break;
} else if (i == 4) {
is_pwd_correct = true;
cout << endl << "Your password is correct" << endl;
}
}
} while (!is_pwd_correct && n < 3);
}
However I would recommend using string instead of char array, and do not use conio.h which is Windows only. Also it would be a good idea to add handling of password size.
Upvotes: 2