Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
#include<iostream>
using namespace std;
main ()
{
	//Local variable declaration
	string password = "Pretty BOY";
	string input;
	
	//Input data
	cout<<"Input password: ";
	cin>>input;
	
	//Process to check condition
	if (input == password)
	{
		cout<<"Access Granted.";
	}
	else 
	{
		cout<<"Access Denied, incorrect password.";
	}
}


What I have tried:

i dont know what's the problem
Posted
Updated 5-Dec-18 22:18pm
v2

Look at what cin does: Basic Input/Output - C++ Tutorials[^] and you will see:
Quote:
However, cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.

To get an entire line from cin, there exists a function, called getline, that takes the stream (cin) as first argument, and the string variable as second.
 
Share this answer
 
The blank is a separator for cin so, if the user enters (correctly) 'Pretty BOY', the variable input would be assigned with 'Pretty'. You may fix it reading the whole line:
C++
#include<iostream>
using namespace std;
int main ()
{
  //Local variable declaration
  string password = "Pretty BOY";
  string input;

  //Input data
  cout<<"Input password: ";
  getline(cin, input);


  //Process to check condition
  if (input == password)
  {
    cout<<"Access Granted.\n";
  }
  else
  {
    cout<<"Access Denied, incorrect password.\n";
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900