C++ -- 这段代码有什么问题
C++ -- what is wrong with this code?
我似乎无法弄清楚这里的问题是什么:
if(temperature<=-173)
cout << "Ethyl alcohol will freeze" << endl;
else if (temperature >-173 && temperature<172)
cout << "Ethyl alcohol will be liquid" << endl;
else(temperature>=172)
cout << "Ethyl alcohol will boil" << endl;
我得到的错误是
error: expected ';' before 'cout'
=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===
更改:
else(temperature>=172)
只是:
else // temperature >= 172
前者不是一个有效的if
陈述,因为它没有if
,无论如何,你已经涵盖了固相和液相,所以唯一剩下的就是气体(尽管有奇怪的等离子体状态)。
您收到模糊错误消息的原因是:
else(temperature>=172)
实际上非常有效(就像42;
有效,但同样无用),执行(temperature>=172)
作为else
情况,它只是评估条件并将其丢弃。
但是,在这种情况下,它应该在下一个语句 cout <<
之前以分号结尾。因此错误:
expected ';' before 'cout'
您不能直接在"else"之后添加条件部分。
更改代码以匹配以下内容:
if(temperature<=-173)
cout << "Ethyl alcohol will freeze" << endl;
else if (temperature >-173 && temperature<172)
cout << "Ethyl alcohol will be liquid" << endl;
else
cout << "Ethyl alcohol will boil" << endl;
确切的问题是您在else
和您的状况(temperature>=172)
之间缺少if
。
但你也可以无条件地写:
if(temperature<=-173)
cout << "Ethyl alcohol will freeze" << endl;
else if (temperature >-173 && temperature<172)
cout << "Ethyl alcohol will be liquid" << endl;
else
cout << "Ethyl alcohol will boil" << endl;
所以详细阐述上面的答案,他的意思是:
if(temperature<=-173)
cout << "Ethyl alcohol will freeze" << endl;
else if (temperature >-173 && temperature<172)
cout << "Ethyl alcohol will be liquid" << endl;
else if (temperature >= 172)
cout << "Ethyl alcohol will boil" << endl;
但是你最好接受我的第一个答案并节省几个字节的处理。
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了