Write a program to compare two strings. One of the strings should be already stored in your program as password. Now take user input for a guess and compare this input with the pre stored password. If both of them match then print “Valid password” otherwise print “Sorry! Wrong password”.
#include <iostream>
#include <string>
using namespace std;
int main(){
const string correctPassword="password";
string userPassword;
cout<<"Enter the password: ";
getline(cin,userPassword);
//print "Valid password" otherwise print "Sorry! Wrong password".
if(userPassword.compare(correctPassword)==0){
printf("\nValid password\n\n");
}else{
printf("\nSorry! Wrong password\n\n");
}
system("pause");
return 0;
}
Comments
Leave a comment