判断手动输入的字符串和给定的宏常量是否相等

怎么判断手动输入的字符串和给定的宏常量是否相等 比如给定#define password c123456
然后我让用户输入一串字符 那么如何判断该字符串和password是否相等呢

直接用等号即可

#include <iostream>
using namespace std;
#define password "abc123"
int main(int argc, char const *argv[])
{
    string s;
    cin >> s;
    cout << s << endl;
    cout <<( s == password);
}

img

有帮助望采纳~

使用strcmp函数比较两个字符串

你题目的解答代码如下:

#include<iostream>
#include<cstring> //使用strcmp函数要引入<cstring>头文件
using namespace std;
#define password "c123456"
// 宏常量定义字符串要加双引号
int main()
{

    char s[100];
    cin.getline(s, 100);
    if (strcmp(s,password)==0)
    {
        cout << "相等" << endl;
    }
    else
    {
        cout << "不相等" << endl;
    }
    return 0;
}

如果是 string 字符串对象可以直接用==比较

#include<iostream>
using namespace std;
#define password "c123456"
// 宏常量定义字符串要加双引号
int main()
{

    string s;
    getline(cin, s);
    if (s == password)
    {
        cout << "相等" << endl;
    }
    else
    {
        cout << "不相等" << endl;
    }
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

#define password "c123456"

std::string str;
std::cin >> str;
if (str == password) {
    // ...
} else {
    // ...
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632