Hi, I've been following a youtube-based tutorial on a c++ program using Eclipse Luna, but despite seeming to have done everything right, I still get an unwanted output. I don't know if there's something inherently wrong with the construction of my algorithm, or if there's something more subtle going on behind the scenes. In a nutshell, I'm supposed to get the final output, "A dandelion will grow", and nothing more. Here's the code in it's entirety (don't worry, it's fairly short). I would have inserted an image, but I don't know how. Thank you for your help.
//A red seed will grow into a flower when planted in soil
//temperatures above 75 degrees, otherwise it will grow into
//a mushroom. Assuming the temperature meets the conditions
//for growing a flower, planting the red seed in wet soil
//will produce a sunflower, and planting the red seed in dry
//soil will produce a dandelion.
//A blue seed will grow into a flower when planted in soil
//temperatures ranging from 60 to 70, otherwise it will grow
//into a mushroom. Assuming the temperature meets the conditions
//for growing a flower, planting the blue seed in wet soil will
//produce a dandelion and planting the blue seed in dry soil
//will produce a sunflower.
#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 mushroom will grow.\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 dandelion will grow.\n";
}
}
//If the soil is dry
if (soilMoisture == "dry")
{
//Output sunflower
cout << "A sunflower will grow.\n";
}
//Otherwise
}
else
{
//Output mushroom
cout << "A mushroom will grow.\n";
}
}
> I'm supposed to get the final output, "A dandelion will grow", and nothing more.
¿and what do you get?
your program ask for input, it would have been a nice detail to provide the one that is giving you issues.
About your problem, do an step-by-step run and watch carefully the lines that get executed.
you don't seem to understand to what condition your else statements are referring to.
Sorry, I forgot to add some of that important information. The console outputs and inputs are supposed to look like the following:
Enter the seed color (red or blue):
red
Enter the temperature (F):
85
Enter the soil moisture (wet or dry):
dry
A dandelion will grow.
------
But instead I get the following console information, which incorrectly ends by adding, "A mushroom will grow":
Enter the seed color (red or blue):
red
Enter the temperature (F):
85
Enter the soil moisture (wet or dry):
dry
A dandelion will grow.
A mushroom will grow.
-----
So I need it to work so that only a dandelion will grow, not a dandelion and mushroom. The input that seems to give me issues is when I input the word, "dry" after being prompted to input the soil moisture. Also, when I debug, the value doesn't show for "seedColor", which I think is strange. Perhaps it's not registering the value? Thanks again for your help.
#include <iostream>
#include <string>
usingnamespace 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 mushroom will grow.\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 dandelion will grow.\n";
}
}
//If the soil is dry
if (soilMoisture == "dry")
{
//Output sunflower
cout << "A sunflower will grow.\n";
}
//Otherwise
}//this terminates the if for the blue seed
else // (seed color is not blue)
{
//Output mushroom
cout << "A mushroom will grow.\n";
}
}
Thank you!! What happened was that the curly brace you mentioned was indeed unnecessary, but I also had to add another curly brace between the last and second to the last one. I didn't realize that the "else" statement is part of the if statement!