5
\$\begingroup\$

I've already done quite a bit of cleaning up. For example, the strings 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 <iostream>
#include <string>
using namespace std;

int main()
{
    string seedColor, soilMoist, redCheck, wetCheck;
    int soilTemp;

    redCheck = "Red";
    wetCheck = "Wet";
    cout << "Enter the seed type (Blue/Red): ";
        getline(cin, seedColor);

    cout << "Enter the soil temperature: ";
        cin >> soilTemp;

    cout << "Is the soil dry or wet? ";
        cin >> 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!";
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! We're glad you found our site. It's best to write a title that will say something about what your code does, as you can see we have made a few edits. I hope you get some great reviews! \$\endgroup\$ – Phrancis Jan 30 '15 at 23:00
  • \$\begingroup\$ @Phrancis Alright, thanks! The title makes more sense now, yes xD \$\endgroup\$ – Jack Jan 31 '15 at 8:39
5
\$\begingroup\$

Just some quick pointers here to get the more obvious details out of the way. First, you should not use 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 << "It's a sunflower!";
else
    cout << "It's a dandelion!";

Should be:

if (soilMoist == wetCheck)
{
    std::cout << "It's a sunflower!";
}
else
{
    std::cout << "It's a dandelion!";
}

Right here, you should not indent your braces:

if (soilTemp > 75)
    {
    if (soilMoist == wetCheck)
        cout << "It's a sunflower!";
    else
        cout << "It's a dandelion!";
    }

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 << "Enter the soil temperature: ";
    cin >> 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 << "Is the soil dry or wet? ";
    }
    else
    {
        std::cout << "Invalid response.\nIs the soil dry or wet? ";
    }

    std::cin >> input;

} while (input != "Wet" && input != "wet");
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Most of these are stylistic preferences and neither right nor wrong. I would elaborate on your point about verifying input. Specifically that he is prompting for wet (lowercase w) but expecting Wet(uppercase W). \$\endgroup\$ – Kevin Jan 31 '15 at 2:58
  • \$\begingroup\$ @Hosch250 I heard that I used braces in a weird way. I thought the indents didn't matter at all, they just help me keeping track of what output belongs to what if or else, like I said, only my 3rd week so I lose track of stuff easily. I know I should've used braces around if and else, but it just didn't seem neccesary right now (bad coding, I know). And finally the using namespace std; was just recommended everywhere on tutorials and stuff, so I didn't really think it would be bad, but I'll make sure to change it. Thanks! \$\endgroup\$ – Jack Jan 31 '15 at 8:07
  • \$\begingroup\$ @Kevin How would I verify the input? Because indeed, when I input "wet" (lowercase) it will see it as false and go to else (dry). \$\endgroup\$ – Jack Jan 31 '15 at 8:08
  • \$\begingroup\$ @Hosch250 Ah I see know what you mean with indenting the braces, it looks a lot better when they're back one tab! :D \$\endgroup\$ – Jack Jan 31 '15 at 8:25
  • \$\begingroup\$ @jack there's a couple of things you could do. One way would be to compare the input to your two expected inputs and then re-prompt in a while loop until it matches one of the two. You could also be more forgiving and compare the input to Wet or wet. Also, as for the brackets and indenting, the important thing is to be clean and consistent so it is easy to read. Also, if you are working with existing code, go with the style that is already there, don't mix in a different style. \$\endgroup\$ – Kevin Jan 31 '15 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.