patterncppMinor
Seed to flower/mushroom
Viewed 0 times
mushroomseedflower
Problem
I've already done quite a bit of cleaning up. For example, the strings
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 a red
seed in wet soil will produce a sunflower and planting a red seed in
dry soil will produce dandelion. A blue seed will grow into a flower
when planted in soil temperatures ranging from 60 to 70 degrees,
otherwise it will grow into a mushroom. Assuming the temperature meets
the conditions for growing a flower, planting a blue seed in wet soil
will produce a dandelion and planting the blue seed in dry soil will
produce a sunflower. Write a program that will ask the user to input
the seed color, the soil temperature and whether the soil is wet or dry
and then output the plant that will grow.
``
dryCheck and blueCheck no longer exist, instead they're replaced with "else". But I think this could be done 10x better, so if you know how (no advanced C++ lingo etc), that would be great!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 a red
seed in wet soil will produce a sunflower and planting a red seed in
dry soil will produce dandelion. A blue seed will grow into a flower
when planted in soil temperatures ranging from 60 to 70 degrees,
otherwise it will grow into a mushroom. Assuming the temperature meets
the conditions for growing a flower, planting a blue seed in wet soil
will produce a dandelion and planting the blue seed in dry soil will
produce a sunflower. Write a program that will ask the user to input
the seed color, the soil temperature and whether the soil is wet or dry
and then output the plant that will grow.
``
#include
#include
using namespace std;
int main()
{
string seedColor, soilMoist, redCheck, wetCheck;
int soilTemp;
redCheck = "Red";
wetCheck = "Wet";
cout > soilTemp;
cout > soilMoist; // I tried another "getline(cin, soilMoist), but that conflicts with the cin >> soilTemp; (line 30)
if (seedColor == redCheck)
{
if (soilTemp > 75)
{
if (soilMoist == wetCheck)
cout << "It's a sunflower!";
else
cout << "It's a dandelion!";
}
else
cout << "It's a mushroom!";
}
else
{
if (60 < soilTemp && soilTemp < 70)
{
if (soilMoist == wetCheck)
cout << "It's a dandelion!";
else
cout << "It's a sunflower!";
}
else cout << "It's a mushroom!";
}
}
`Solution
Just some quick pointers here to get the more obvious details out of the way. First, you should not use
Second, you should use braces around your
{
std::cout
Right here, you should not indent your braces:
do {
if (input == "")
{
std::cout > input;
} while (input != "Wet" && input != "wet");
`
using namespace std;, but rather explicitly state which namespace you are using like this std::cout. This prevents namespace clashes, and is discussed in great detail here.Second, you should use braces around your
if/else statements and loops even when there is only one statement after it:if (soilMoist == wetCheck)
cout
Should be:
if (soilMoist == wetCheck){
std::cout
Right here, you should not indent your braces:
if (soilTemp > 75)
{
if (soilMoist == wetCheck)
cout
You do not do this in other areas, so I don't know why you did it here.
You should also not indent your cins:
cout > soilTemp;
You should have your prompts always specify exactly what the input should be. Here, it looks as if you expect lowercase input: "Is the soil dry or wet? " However, the program checks for uppercase input. The best way to fix this would be to allow both "wet" or "Wet".
Otherwise, this looks pretty good. However, one way you may want to consider expanding your program in is ensuring the user put in the right values instead of merely assuming the right values will be input. You could do this with a do-while loop, among other ways.
This code is an example of what I mean:
string input = "";do {
if (input == "")
{
std::cout > input;
} while (input != "Wet" && input != "wet");
`
Context
StackExchange Code Review Q#79144, answer score: 5
Revisions (0)
No revisions yet.