c++ - 无法编译,需要左值,在函数 main 中

标签 c++

我想编译它,但它给了我错误,即使我更改了引号,头文件中是否有任何错误,请告诉我

#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/

相关文章:

c++ - 子弹物理因引用无效而崩溃?

c++ - NMAKE : fatal error U1052: file 'Makefile.vc' not found

c++ - 如何从Syslog-NG/Syslog获取数据

c++ - 错误:'class std::result_of 中没有名为 ‘type’ 的类型

c++ - 为什么在使用 ReadFile() 函数时会发生访问冲突?

c++ - clang++ 无法用类型为 'int(*)[dim2]' 的右值初始化类型为 'int (*)[dim2]' 的变量

c++ - 编译 GCC 时错误部分模板特化,但不是 MSVC

c++ - 使用接受的连接作为套接字 (cpp-netlib)

c++ - std::map<struct, int> 我需要析构函数吗?

c++ - 无条件地从构造函数中抛出 *this