Reputation: 11
I need a program that after 1 wrong password it shows certain messege, after 2 wrong tries shows diffrent messege and after 3rd one it shut itself off. Of course after correct password it should turn the program on.
It has to contain do...while.
Here's the part of the code that should operate passwords (if there are any difficulties with language I can translate but it's just passwords and messeges to show):
int licznik = 0; // licznik => counter
string ha; // ha short hand for haslo => passowrd
cout << "Podaj haslo:" << endl; // Prodaj => to try, to pass
cin >> ha;
if (ha != "haslo" && licznik < 3)
cout << "Haslo bledne, sprobuj jeszcze raz!" << endl;
// blende => wrong
// jeszcze => once
// raz => again
else
cout << "Haslo prawidlowe!";
// prawidlowe => correct
do {
licznik++;
if (licznik <= 2)
cout << "Ostatnia szansa!!!!!" << endl;
// Ostatnia => final
// szansa => chance
else {
exit(0);
}
} while (ha == "haslo");
Upvotes: 0
Views: 485
Reputation:
I am Slavic descent, but I cannot read polish thus it is hard for me to understand exactly what you mean, and what you want, but my best guess is that you wanted something like this:
#include <iostream>
using namespace std;
int main (void) {
int licznik = 0;
string ha;
cout << "Enter password:" << endl;
do {
if (licznik > 2) {
std::cout << "You entered password too many times";
exit (0);
} else if (licznik > 0)
cout << "You entered wrong password "
<< licznik << " times. Try again: "
<< endl;
cin >> ha;
licznik++;
} while (ha != "haslo");
std::cout << "Success you are logged in" << endl;
return 0;
}
Upvotes: 1
Reputation: 342
Try this, It might be helpful for you to learn
Here I have use multiple goto statement
within if conditions
in do-while loop. Basically goto statement is an unconditional jump statement used for transferring the control of a program. you can also learn goto statement here.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string pass;
int x = 0;
a: // when two tries are left
b: // when 1 try is left
cout << "Enter password : " << ' ';
cin >> pass;
if (pass != "password" && x < 3)
{
cout << "Password incorrect\n";
do
{
x++;
if (x == 1)
{
cout << "please try again (2 try left)\n\n";
goto a;
}
else
if (x == 2)
{
cout << "please try again (1 try left)\n\n";
goto b;
}
else
if (x == 3)
{
cout << "terminating\n\n";
exit(1);
}
} while (x <= 3);
}
else
{
cout << "Password is correct\n\n\n";
}
return 0;
}
Sample Output:
Enter password : World
Password incorrect
please try again (2 try left)
Enter password : prom
Password incorrect
please try again (1 try left)
Enter password : password
Password is correct
Upvotes: 0
Reputation: 7726
The solution is quite easy to achieve even without using statements like goto
or nested conditions in a very simplified manner (read the comments for explanation):
#include <iostream>
int main(void) {
std::string password;
int chances = 3;
do {
// Get the user input (could be multispaced)
std::getline(std::cin, password);
if (password == "haslo")
break;
// Conditioning the attempts
if (chances != 0) {
std::cout << "You have " << chances-- << " left\n";
continue;
} else {
std::cerr << "Password invalid.\n";
return -1;
}
} while (true);
std::cout << "Access granted.\n";
return 0;
}
Here's a sample test case:
rohanbari@genesis:~/stack$ ./a.out
hello
You have 3 left
there
You have 2 left
haslo
Access granted.
Upvotes: 1