确定密码的程序错误或正确。我的源代码有什么问题? [英] A program that determines a password is either wrong or correct. What is wrong in my source code?

查看:60
本文介绍了确定密码的程序错误或正确。我的源代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我的尝试:



i不知道是什么问题



What I have tried:

i dont know what's the problem

推荐答案

看看 cin 确实:基本输入/输出 - C ++教程 [ ^ ]你会看到:

Look at what cin does: Basic Input/Output - C++ Tutorials[^] and you will see:
引用:

但是,cin提取总是将空格(空格,制表符,换行符......)视为终止提取的值,因此提取字符串意味着始终提取单个字,不是短语或整个句子。



要从cin获取整行,有一个名为getline的函数,它将流(cin)作为第一个参数,字符串变量为秒。

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.


空白是 cin 的分隔符,所以,如果我们er输入(正确)'Pretty BOY',变量 input 将被赋予'Pretty'。您可以修改它阅读整行:

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";
  }
}


这篇关于确定密码的程序错误或正确。我的源代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆