c++程序错误(修复)

c++ program error (fix)

本文关键字:修复 错误 程序 c++      更新时间:2023-10-16

需要帮助修复此代码我对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;
}

您忘记了"}"的地方并不明显。它可能在最后一个之前,也可能在其他任何地方。

您应该正确地缩进代码。比你自己看到你的错误。