我不太喜欢编程,实际上我已经开始并给自己做作业了,可以随意说我是菜鸟。

这是问题陈述:

您可以种植两种种子之一(蓝色或红色)
在温度高于75度的土壤中种植时,红色会长成花,否则,如果温度符合条件,则它将长成蘑菇,要在花朵上种植花,在潮湿的土壤中种植红色种子会产生向日葵,然后种植红色种子。在干燥的土壤中会产生dandiliom。
在土壤温度下,蓝色种子会在花朵中生长。从60-70 F度。或者是蘑菇。在潮湿的土壤中,在干燥的蒲公英中

这是代码:

*

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string plantedSeed = "";
    string seedColor = "";
    cout << "What color will the seed be? (red/blue): \n";
    getline(cin, seedColor);
    int soilTemperature = 0;
    cout << "What temperature will the soil have?\n";
    cin >> soilTemperature;
    if (seedColor == "red")
    {
        if (soilTemperature >= 75)
            plantedSeed = "mushroom";
        if (soilTemperature < 75)
        {
            string seedState = "";
            cout << "Enter the state of the soil in which the seed is plantet to (wet/dry)\n";
            getline(cin, seedState);
            if (seedState == "wet")
                plantedSeed = "sunflower";
            if (seedState == "dry")
                plantedSeed = "dandelion";
        }
    }
    if(seedColor == "blue")
    {
        if (soilTemperature >= 60 && soilTemperature <= 70)
            plantedSeed = "mushroom";
        else
        {
            string seedState = "";
            cout << "Enter the state of the soil in which the seed is plantet to (wet/dry)\n";
            getline(cin, seedState);
            if (seedState == "wet")
                plantedSeed = "dandelion";
            if (seedState == "dry")
                plantedSeed = "sunflower";
        }
    }
    cout << "The planted seed has transformed into: " << endl;
    cout << plantedSeed << endl;
    system("pause");
    return 0;
}


*
问题在于程序拒绝进入if(soilTemperature
if (seedColor == "red")
    {
        if (soilTemperature >= 75)
            plantedSeed = "mushroom";
        if (soilTemperature < 75)
        {
            string seedState = "";
            cout << "Enter the state of the soil in which the seed is plantet to (wet/dry)\n";
            getline(cin, seedState);
            if (seedState == "wet")
                plantedSeed = "sunflower";
            if (seedState == "dry")
                plantedSeed = "dandelion";
        }
    }


蓝色也一样。

最佳答案

读取温度后,您需要忽略\n

cout << "What temperature will the soil have?\n";
cin >> soilTemperature;
cin.ignore();


读取温度后,在标准输入中有此行尾。然后,在下面的getline中读取空行。当然,您错了,程序进入第二条语句,但是getline直接以空行结束。

关于c++ - 该程序拒绝进入第二条语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38657195/

10-13 03:37