Click here to Skip to main content
13,800,885 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
#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 5-Dec-18 22:54pm
Updated 5-Dec-18 23:18pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
#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";
  }
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181215.1 | Last Updated 6 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100