Answer to Question #188866 in C++ for Asad

Question #188866

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”.



1
Expert's answer
2021-05-06T12:30:09-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS