c++;程序错误(修复) 需要帮助修复这个代码,我对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; } #包括 #包括 使用名称空间std; int main() { //获得种子颜色 字符串seedColor=“”; 种子颜色; //临时工 内部温度=0; 温度; //获取土壤水分 弦土水分=”; 土壤水分; //如果红种子 如果(种子颜色=“红色”) { //如果温度>=75 如果(温度>=75) { //如果土壤是湿的 如果(土壤湿度=“湿”) { //向日葵产量 你忘了“}”的地方并不明显。 e、 它可能在最后一个之前,但也可能在其他任何地方
您应该正确缩进代码。这样您就可以自己看到错误了。简短回答:您没有正确关闭c++;程序错误(修复) 需要帮助修复这个代码,我对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; } #包括 #包括 使用名称空间std; int main() { //获得种子颜色 字符串seedColor=“”; 种子颜色; //临时工 内部温度=0; 温度; //获取土壤水分 弦土水分=”; 土壤水分; //如果红种子 如果(种子颜色=“红色”) { //如果温度>=75 如果(温度>=75) { //如果土壤是湿的 如果(土壤湿度=“湿”) { //向日葵产量 你忘了“}”的地方并不明显。 e、 它可能在最后一个之前,但也可能在其他任何地方,c++,syntax,C++,Syntax,您应该正确缩进代码。这样您就可以自己看到错误了。简短回答:您没有正确关闭if(seedColor==“red”)和if(seedColor==“blue”)块(除非一个种子可能同时是红色和蓝色的,并且在最后省略了一些代码,我认为情况并非如此) 更长、更具建设性的回答:您面临的这个问题基本上是因为缩进问题,这使得您很难直观地识别是否发生了这种情况。您可能习惯于在编写代码时手动执行此操作,但有一些工具可以用于此。 假设您没有使用完整的IDE,如EclipseCDT或VisualStudio(否则缩进
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;
}
#包括
#包括
使用名称空间std;
int main(){
//获得种子颜色
字符串seedColor=“”;
种子颜色;
//临时工
内部温度=0;
温度;
//获取土壤水分
弦土水分=”;
土壤水分;
//如果红种子
如果(种子颜色=“红色”){
//如果温度>=75
如果(温度>=75){
//如果土壤是湿的
如果(土壤湿度=“湿”){
//向日葵产量
您可能错过了if条件的(}
)
一个位于if(seedColor==“red”)
和其他atif(seedColor==“blue”)
#包括
#包括
使用名称空间std;
int main()
{
//获得种子颜色
字符串seedColor=“”;
种子颜色;
//临时工
内部温度=0;
温度;
//获取土壤水分
弦土水分=”;
土壤水分;
//如果红种子
如果(种子颜色=“红色”)
{
//如果温度>=75
如果(温度>=75)
{
//如果土壤是湿的
如果(土壤湿度=“湿”)
{
//向日葵产量
如果你不能正确地缩进你的代码,那么一切都会变得清晰。事实上,同样的道理,如果你能客观地指出错误,那么你指出这一点的方式会更好。现在这是这个问题答案中最好的答案。值得a+1。谢谢你,这对我很有帮助。我已经找了一整天了,这个答案是这真是太神奇了!这个答案帮了我很多,我喜欢它。它让我更容易理解我把“}”放错了什么地方。
#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;
}