c++ - 我的 if 语句无法正常运行

标签 c++ if-statement

无论我在 if 语句中键入什么,代码都不会像我的教授希望的那样工作。

我的助教说这是一个语法错误,但没有为我提供更多的背景信息。

"The syntax you used is incorrect. If you are comparing the initials to abc for example, you would say if(initials == "abc"). Likewise, do the same for each condition."

我不太明白他的意思。

如果我更改 if 语句以添加引号,它总是拒绝访问。如果我保持原样,它总是会授予访问权限。

这是我的代码:

#include <iostream>
#include <string>
using namespace std;

int main(){

    cout<<"Gain entry with your user information and password." <<endl;
    cout<<"For security purposes, the information will not be echoed to the screen." <<endl;
    cout<<"Please enter your initials: " <<endl;

    string initials;
    cin>>initials;

    cout<<"Please enter your age: " <<endl;

    int age;
    cin>>age;

    cout<<"Please enter your password: " <<endl;

    string password;

    if ( password == initials,age) {
      cout<<"Access granted!" <<endl;
    }

    else {
      cout<<"Access denied!" <<endl;
    }


     return 0;  
}

内容如下:

==================== YOUR OUTPUT =====================                                                                                                                                                                            
0001: Gain~entry~with~your~user~information~and~password.                                                                                                                                                                         
0002: For~security~purposes,~the~information~will~not~be~echoed~to~the~screen.                                                                                                                                                    
0003: Please~enter~your~initials:                                                                                                                                                                                                 
0004: Please~enter~your~age:                                                                                                                                                                                                      
0005: Please~enter~your~password:                                                                                                                                                                                                 
0006: Access~granted!                                                                                                                                                                                                             

=================== MISMATCH FOUND ON LINE 0006: ===================                                                                                                                                                              
ACTUAL  : Access~granted!                                                                                                                                                                                                         
EXPECTED: Access~denied!                                                                                                                                                                                                          
======================================================                                                                                                                                                                            

Adjust your program and re-run to test.                                                                                                                                                                                           

Test 2 failed                                                                                                                                                                                                                     
ccc_0bc38e1b3b_10207@runweb5:~$ ^C                                                                                                                                                                                                
ccc_0bc38e1b3b_10207@runweb5:~$

最佳答案

根据您的问题,为什么您的代码总是返回已授予的访问权限,这是 if 语句中 C/C++ 中逗号 (,) 运算符的行为。 本质上,你是如何写的,它分为两部分: 1.密码与初始值的字符串比较 2.年龄变量的评价

现在,“if”语句中的逗号的行为使其忽略第一个表达式,即逗号之前的表达式,并评估第二个表达式以在“if”子句中做出决定。

对于您来说,可能总是键入非零的“年龄”,计算的表达式始终为真并且“访问被授予”。 只是为了好玩,尝试您的原始代码并将“年龄”输入为零,它应该转到“拒绝访问”。

关于c++ - 我的 if 语句无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58615031/

相关文章:

c++ - 在 IOKit 代码中使用 `decltype`

c++ - 如何在 C++ 中以毫秒为单位获取系统时间(不是自纪元以来)?

c - 如果参数不是文件,则从标准输入读取

if-statement - 已声明但未使用 - 如何更新 IF 语句中的变量

r - 为什么Ifelse无法取代NA?

c++ - 如何创建 CMakeLists.txt 以编译 PNaCI 程序?

c++ - std::remove_if 中的 const 参数

Javascript - 带有对象的函数

matlab - if then 的单行

c# - 命名管道 C# 服务器 C++ .dll 客户端不工作?