c ++程序错误(修复)

时间:2015-02-21 12:09:20

标签: c++ syntax

需要帮助修复此代码我对c ++完全不熟悉:我在输入错误结束时收到预期的'}'。今天我站起来试图解决这个问题,我在Windows 7上使用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;
    }

3 个答案:

答案 0 :(得分:3)

简答:您没有正确关闭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;
}

答案 1 :(得分: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;
}

答案 2 :(得分:-1)

在你忘记'}'的地方并不是很明显。 例如,它可能在最后一个之前,但也可能在其他地方。

您应该正确缩进代码。比你自己看错了。