我收到一个我不理解的错误,因为我是C++的新手.该if陈述在这种情况下有问题:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string username;
int password;
cout << " Welcome User, Please Login." << endl;
//Username
cout << "Username: ";
cin >> username;
//Password
cout << "\nPassword: ";
cin >> password;
if (username == 'admin' && password == '852456')
cout << "Welcome." <<endl;
else
cout << "Wrong credentials." <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
if (username == 'admin' && password == '852456')
应该
if (username == "admin" && password == 852456)
使用'admin'没有意义,这些标记仅适用于诸如此类的字符char alpha = 'a'."用于字符串(多个字符).数字不需要其中任何一个,它们只是数字.