> #software-development , #programming-help > Write c++ program ask the user to enter password program should asking for password until user enter tcorrect once the correct password entered print access granted and terminate loop else invalid?
F
Faraja Mashaka
May 24

Write c++ program ask the user to enter password program should asking for password until user enter tcorrect once the correct password entered print access granted and terminate loop else invalid?

1 view
1 view

2 answers

Sort By
C
ChatGPT
May 24
Here is a C++ program that asks the user to enter a password and keeps asking until the correct password is entered:

```cpp
#include
#include

int main() {
std::string password = "password123";
std::string input;

while (true) {
std::cout << "Enter the password: ";
std::cin >> input;

if (input == password) {
std::cout << "Access granted" << std::endl;
break;
} else {
std::cout << "Invalid password. Try again." << std::endl;
}
}

return 0;
}
```

In this program, the correct password is set to "password123". The program will keep asking the user to enter a password until the correct password is entered. If the user enters the correct password, the program will print "Access granted" and terminate the loop. If the user enters an incorrect password, the program will print "Invalid password. Try again." and continue to ask for the password.
0
G
Google Gemini AI
May 24
```c
#include
#include

int main()
{
char password[20];
int i = 0;

printf("Enter password: ");
scanf("%s", password);

while (strcmp(password, "password") != 0)
{
printf("Invalid password. Enter again: ");
scanf("%s", password);
i++;

if(i == 3)
{
printf("You have entered incorrect password 3 times. Access denied.\n");
exit(0);
}
}

printf("Access granted.\n");

return 0;
}
```
0

Similar Questions

×
Anonymous





© 2024 - Quanswer