Passwordcpp Write a while loop to be used to test a passwo

// Password.cpp //

Write a while loop to be used to test a password. The password is \"secret\" and the code within the loop is //executed until the user inputs the correct password.

#include #include using namespace std; int main(){ string passwd; string answer = \"secret\"; int count = 0; cout << \"Please enter your password: \"; cin >> passwd; while (passwd != answer) { cout << \"Wrong password\"; } cout << \"Welcome Back\ \"; return 0; }

I need help comparing two strings in order to verify a password with the string \"secret\" in C++

Solution

#include <iostream>

#include<cstring>

using namespace std;
int main(){
string passwd;
string answer = \"secret\";
int count = 0;
cout << \"Please enter your password: \";
cin >> passwd;
while (passwd != answer) {
cout << \"Wrong password. \";
cout << \"Please enter your password: \";
cin >> passwd;
count++;

}
cout << \"Welcome Back\ \";
return 0;
  
}

Output:


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site