C++ 程序错误(修复)
c++ program error (fix)
需要帮助修复此代码我是 c++ 的新手:我在输入错误结束时得到预期的“}”。我今天站了一会儿试图解决这个问题,我在 windows 7 上使用 eclipse,但是对于 none 我提出的所有想法都帮助我解决了这个问题。
#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;
}
您忘记“}”的地方并不明显。
例如,它可以在最后一个 else 之前,但也可以在其他任何地方。
您应该正确地缩进您的代码。比你自己会看到你的错误。
简短回答: 您没有正确关闭 if(seedColor == "red")
和 if(seedColor == "blue")
块(除非种子可能同时是红色和蓝色最后省略了一些代码,我认为情况并非如此。
更长且更有建设性的回答:您遇到这个问题主要是因为缩进问题,使您很难从视觉上识别出这种情况正在发生。您可能习惯于在编写代码时手动执行此操作,但是有一些工具可以做到这一点。
假设您没有使用完整的 IDE,例如 Eclipse CDT 或 visual studio(否则 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;
}
需要帮助修复此代码我是 c++ 的新手:我在输入错误结束时得到预期的“}”。我今天站了一会儿试图解决这个问题,我在 windows 7 上使用 eclipse,但是对于 none 我提出的所有想法都帮助我解决了这个问题。
#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;
}
您忘记“}”的地方并不明显。 例如,它可以在最后一个 else 之前,但也可以在其他任何地方。
您应该正确地缩进您的代码。比你自己会看到你的错误。
简短回答: 您没有正确关闭 if(seedColor == "red")
和 if(seedColor == "blue")
块(除非种子可能同时是红色和蓝色最后省略了一些代码,我认为情况并非如此。
更长且更有建设性的回答:您遇到这个问题主要是因为缩进问题,使您很难从视觉上识别出这种情况正在发生。您可能习惯于在编写代码时手动执行此操作,但是有一些工具可以做到这一点。 假设您没有使用完整的 IDE,例如 Eclipse CDT 或 visual studio(否则 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;
}