我想编译它,但它给了我错误,即使我更改了引号,头文件中是否有任何错误,请告诉我
#include<iostream.h>
#include<conio.h>
void main()
{
char st[20];
cin>>st;
cout<<st<<endl;
if (st = 'a')
cout<<"This is a";
if (st = 'b')
cout<<"This is b";
getch();
}
最佳答案
以下说法不太正确:
if (st = 'a')
if (st = 'b')
首先,=
是赋值而不是比较。其次,'a'
和 'b'
是 char
而不是字符串文字。
上面的正确写法是
if (strcmp(st, "a") == 0)
if (strcmp(st, "b") == 0)
也就是说,我鼓励您不再使用 C 字符串,而是使用 std::string
。
关于c++ - 无法编译,需要左值,在函数 main 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16117326/