当我输入 srtike 时"access granted"没有出现
When i enter srtike "access granted" doesn't appear
#include<iostream.h>
#include<string.h>
void main() {
char a[20];
cout<<"Please enter the required password"<<endl;
cin>>a;
if (a=="srtike"){
cout<<"Access granted!"<<endl;
}
gets(a);
}
不能将数组与字符串进行比较,结果是数组衰变成指向数组第一个字符的指针,然后将该指针与指向字符串的指针进行比较,该指针始终为false。
相反,如果你想使用C字符串,你应该使用strcmp
来比较字符串:
if (strcmp(a, "srtike") == 0)
{
// the contents of a equals "srtike"
...
}
否则,我建议你看看std::string
,它可以像你一样进行比较,因为operator==
是重载的。
相关文章:
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- Simulink "Access Violation"写入 C++ lambda 函数捕获列表中的 PWork 变量
- Qt QXmlStreamReader Access Violation
- "Access is Denied" U盘上的创建文件()
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- 例外:'Access violation reading location'
- 向量数组"Cannot access memory at address"
- Windeployqt不会运行,"Access is denied."
- 我在发布模式下运行时收到"Access violation reading location"错误 - C++
- Access C++ Enum from Swift
- Java 本机访问代码错误:"Invalid memory access"
- 如何获得私人会员的"read access"?
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- C++:分析包含表达式的字符串"access to the multidimensinal array"
- 如何修复我的 c++ 毕达哥拉斯三重查找器中的'access violation reading location'错误?
- C 和WXWIDGETS DLL创建并与Microsoft Access一起使用
- 矩阵 - "Access violation writing location 0x00900B0C" - C++ 中的错误
- 当我输入 srtike 时"access granted"没有出现