c++程序错误(修复)
c++ program error (fix)
需要帮助修复此代码我对c++完全陌生:在输入错误结束时,我得到了预期的"}"。今天我站了一会儿试图解决这个问题,我在windows7上使用eclipse,但尽管我有很多想法,但没有一个能帮助我解决这个问题。
#include <iostream>
#include <string>
using namespace std;
int main ()
{
// Get seed color
string seedColor = "";
cout << "Enter the seed color (red or blue): n";
cin >> seedColor;
// Get temp
int temp = 0;
cout << "Enter the temperature (F): n";
cin >> temp;
// Get the soil moisture
string soilMoisture = "";
cout << "Enter the soil moisture (wet or dry): n";
cin >> soilMoisture;
// if red seed
if(seedColor == "red")
{
// If temp >= 75
if(temp >= 75)
{
// if the soil is wet
if(soilMoisture == "wet")
{
// output sunflower
cout << "A sunflower will grow.n";
}
// if the soil is dry
if(soilMoisture == "dry")
{
// output dandelion
cout << "A dandelion will grow.n";
}
}
// otherwise
else
{
// output mushroom
cout << "a nasty mushroom will form!n";
}
// if blue seed
if(seedColor == "blue")
{
// If temp is between 60 and 70
if(temp >= 60 && temp <= 70)
{
// If the soil is wet
if(soilMoisture == "wet")
{
// output dandelion
cout << "A beautiful dandelion will grow.n";
}
// If the soil is dry
if(soilMoisture == "dry")
{
// output sunflower
cout << "A sunflower will grow out of the earth!n";
}
}
// otherwise
else
{
// output mushroom
cout << "You will produce a mushroom.n";
}
return 0;
}
简短回答:您没有正确关闭if(seedColor == "red")
和if(seedColor == "blue")
块(除非种子可能同时为红色和蓝色,并且末尾省略了一些代码,我认为情况并非如此)。
更长、更具建设性的答案:您面临这个问题的主要原因是缩进问题,这使得您很难从视觉上识别出正在发生这种情况。在编写代码时,您可能会习惯于手动执行此操作,但有一些工具可以做到这一点。假设您没有使用完整的IDE,如EclipseCDT或visualstudio(否则缩进可能已经由IDE处理了)。
如图所示,经过更正并具有正确缩进的代码如下所示。
#include <iostream>
#include <string>
using namespace std;
int main() {
// Get seed color
string seedColor = "";
cout << "Enter the seed color (red or blue): n";
cin >> seedColor;
// Get temp
int temp = 0;
cout << "Enter the temperature (F): n";
cin >> temp;
// Get the soil moisture
string soilMoisture = "";
cout << "Enter the soil moisture (wet or dry): n";
cin >> soilMoisture;
// if red seed
if (seedColor == "red") {
// If temp >= 75
if (temp >= 75) {
// if the soil is wet
if (soilMoisture == "wet") {
// output sunflower
cout << "A sunflower will grow.n";
}
// if the soil is dry
if (soilMoisture == "dry") {
// output dandelion
cout << "A dandelion will grow.n";
}
}
// otherwise
else {
// output mushroom
cout << "a nasty mushroom will form!n";
}
}
// if blue seed
if(seedColor == "blue")
{
// If temp is between 60 and 70
if(temp >= 60 && temp <= 70)
{
// If the soil is wet
if(soilMoisture == "wet")
{
// output dandelion
cout << "A beautiful dandelion will grow.n";
}
// If the soil is dry
if(soilMoisture == "dry")
{
// output sunflower
cout << "A sunflower will grow out of the earth!n";
}
}
// otherwise
else
{
// output mushroom
cout << "You will produce a mushroom.n";
}
}
return 0;
}
您错过了if条件的(}
)。
if(seedColor == "red")
一个
另一个在if(seedColor == "blue")
#include <iostream>
#include <string>
using namespace std;
int main ()
{
// Get seed color
string seedColor = "";
cout << "Enter the seed color (red or blue): n";
cin >> seedColor;
// Get temp
int temp = 0;
cout << "Enter the temperature (F): n";
cin >> temp;
// Get the soil moisture
string soilMoisture = "";
cout << "Enter the soil moisture (wet or dry): n";
cin >> soilMoisture;
// if red seed
if(seedColor == "red")
{
// If temp >= 75
if(temp >= 75)
{
// if the soil is wet
if(soilMoisture == "wet")
{
// output sunflower
cout << "A sunflower will grow.n";
}
// if the soil is dry
if(soilMoisture == "dry")
{
// output dandelion
cout << "A dandelion will grow.n";
}
}
// otherwise
else
{
// output mushroom
cout << "a nasty mushroom will form!n";
}
} // <--
// if blue seed
if(seedColor == "blue")
{
// If temp is between 60 and 70
if(temp >= 60 && temp <= 70)
{
// If the soil is wet
if(soilMoisture == "wet")
{
// output dandelion
cout << "A beautiful dandelion will grow.n";
}
// If the soil is dry
if(soilMoisture == "dry")
{
// output sunflower
cout << "A sunflower will grow out of the earth!n";
}
}
// otherwise
else
{
// output mushroom
cout << "You will produce a mushroom.n";
}
} // <--
return 0;
}
您忘记了"}"的地方并不明显。它可能在最后一个之前,也可能在其他任何地方。
您应该正确地缩进代码。比你自己看到你的错误。
相关文章:
- 如何组织 git 工作流以修复错误并同时引入新功能
- 如何修复错误 2298 和 2563
- 如何修复错误:使用向量查找最小值和最大值进行合并排序
- 修复错误的 JSON 数据
- 在Visual Code 2019中使用graphics.h绘制线条时,如何修复错误E0167和C2019?
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 视觉如何修复错误E0026 C
- 如何修复错误,迭代器未在此范围内声明,并且迭代器未命名类型'
- 如何在c ++中调整数组大小时修复错误?
- 如何修复错误分配C++运行时错误
- 如何修复错误C2259:无法实例化抽象类
- 视觉"如何修复错误 C4996:"_strupr":C++ 中的错误"
- 如何在两个设备之间与TCP通信时修复错误"连接被拒绝"
- 如何修复错误 LNK 2022?
- Microsoft Visual C++, Visual Studio, 如何修复错误: 标识符"GetAsyncKeyState"未定义
- 如何修复错误消息,如"argument list for class template is missing"?
- 如何修复错误"在c ++ UE4中为类似函数的宏调用'TEXT的参数过多
- 如何修复错误:g++.exe:无法使用-c或-S和多个编译指定-o
- 如何修复错误:请求从"void"转换为非标量类型"std::vector<int>"