HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Garden sprinkler system

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gardensystemsprinkler

Problem

I was given this assignment below and wrote the code below that as the solution. My instructor has been blasting me for writing inefficient code for my last several assignments, and rails at me for using strings as I do. She won't show me why its wrong but just states that it is wrong. I need to know why what I am writing here is so wrong and why. Where am I being so inefficient? I am also being blasted for the use of too many strings, what is wrong with using strings and why?

The Assignment:


Design a program that will determine based on the temperature and
amount of rain whether or not the sprinklers should be turned on.


Note: The inputs and outputs below are for the specified data. When I test
the program you will be asked to type in different values during
program execution.



  • There are 3 regions in the Garden: F-Flowers


V-Vegetable B-Berries. Report an INVALID garden choice if F, V or B is
not entered for the garden choice.

  • If the temperature is above 40


degrees then based on the precipitation it can be watered. The Flower
Garden it should be watered when the precipitation is less than 0.15
inches. The Vegetable Garden should be watered when less than 0.375
inches and the Berries should be watered when there is less than 0.425
inches. It should state which garden is being watered and or not.


Example program Outputs

Garden Sprinkler System

What is the temperature in degrees(F)? 78                             
How much precipitation today(in inches)?.1                            
The Garden Divisions are F-Flowers  V-Vegetable B-Berries             
Which do you choose (FVB)? B

Given the temperature is 78 degrees and 0.100 inches of precipitation
today.  The Berries in the Garden will be watered.




OR

```
Garden Sprinkler System

What is the temperature in degrees(F) ?32
How much precipitation today(in inches)?0.2
The Garden Divisions are F

Solution

My thoughts:

  • Your variables are all declared at the beginning of the function: don't do that. Declare variables close to where they are used



  • Your variable names are confusing and give little indication of what they are for



  • You use magic numbers 100, 10, 1 to denote the different types of plants, that's hard to keep track of



  • You have the exact same lines of code over and over again (this is probably what the teacher meant by having too many strings)



  • Your code does not have consistent indentation, that makes it hard to read.



Let's look at a small piece of code:

if (Precip < 0.375){
 cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip<<" inches ";
 cout<< "of precipitation today.";
 cout<< "\nThe "<< Flr <<" in the Garden will be watered.";
 break;
 }
 else{
 cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip <<" inches of";
  cout<<" precipitation today.\nThe "<< Flr <<" in the Garden will NOT be watered.";     
  break;
  }


The first line of code is the same in both cases, we don't need to repeat them, instead we can do:

cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip<<" inches ";
 if (Precip < 0.375){

 cout<< "of precipitation today.";
 cout<< "\nThe "<< Flr <<" in the Garden will be watered.";
 break;
 }
 else{
  cout<<" precipitation today.\nThe "<< Flr <<" in the Garden will NOT be watered.";     
  break;
  }


See that the code is simpler and less repetitive but does the same thing? We can continue. Most of the text inside the code is

cout= 0.375){
     cout << "NOT"
 }

 cout << "be watered.";     
 break;


That way you have less strings, your code is clearer and more "efficient." If you apply this sort of logic several times you should be able to eliminate the same strings appearing multiple times.

Here is what I would for this problem. I may be using techniques you have not learned yet. But, hopefully it will give you some idea of the lack of duplication you should be striving for.

#include 
#include 
#include 

using namespace std;

enum GardenDivision
{
    BERRIES,
    VEGETABLES,
    FLOWERS
};

struct Garden
{
    GardenDivision division;
    int temperature;
    float percipitation;
};

bool read_user_input(Garden & garden)
{
    cout > garden.temperature;
    cout > garden.percipitation;
    cout > division_code;
        switch(division_code)
        {
            case 'F':
            case 'f':
                garden.division = FLOWERS;
                return true;
            case 'V':
            case 'v':
                garden.division = VEGETABLES;
                return true;
            case 'B':
            case 'b':
                garden.division = BERRIES;
                return true;
        }

        cout  40)
        {
            water_by_section(garden);
        }
        else
        {
            cout << "The berries, vegetables, and flowers in the Garden will NOT be watered today.";
            cout << "\nBecause it is too cold.";
        }
    }

    getchar();
    getchar();

    return 0;
}


Good Luck!

Code Snippets

if (Precip < 0.375){
 cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip<<" inches ";
 cout<< "of precipitation today.";
 cout<< "\nThe "<< Flr <<" in the Garden will be watered.";
 break;
 }
 else{
 cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip <<" inches of";
  cout<<" precipitation today.\nThe "<< Flr <<" in the Garden will NOT be watered.";     
  break;
  }
cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip<<" inches ";
 if (Precip < 0.375){

 cout<< "of precipitation today.";
 cout<< "\nThe "<< Flr <<" in the Garden will be watered.";
 break;
 }
 else{
  cout<<" precipitation today.\nThe "<< Flr <<" in the Garden will NOT be watered.";     
  break;
  }
cout<< "\n\n\n\nGiven the temperature is "<< Tmp <<" degrees and "<< Precip<<" inches ";
 cout<< "of precipitation today.";
 cout<< "\nThe "<< Flr <<" in the Garden will be watered.";
 if (Precip >= 0.375){
     cout << "NOT"
 }

 cout << "be watered.";     
 break;
#include <iostream>
#include <stdio.h>
#include <iomanip>

using namespace std;

enum GardenDivision
{
    BERRIES,
    VEGETABLES,
    FLOWERS
};

struct Garden
{
    GardenDivision division;
    int temperature;
    float percipitation;
};

bool read_user_input(Garden & garden)
{
    cout << setprecision(3) << fixed;
    cout << "Garden Sprinkler System\n\n\n\n";
    cout << "What is the temperature in degrees(F) ? ";
    cin >> garden.temperature;
    cout << "How much precipitation today (in inches)? ";
    cin >> garden.percipitation;
    cout << "The Garden Divisions are F-Flowers  V-Vegetable B-Berries";
    cout << "\nWhich do you choose (FVB)? ";

    for(int counter = 0; counter < 4;counter++)
    {
        char division_code;
        cin >> division_code;
        switch(division_code)
        {
            case 'F':
            case 'f':
                garden.division = FLOWERS;
                return true;
            case 'V':
            case 'v':
                garden.division = VEGETABLES;
                return true;
            case 'B':
            case 'b':
                garden.division = BERRIES;
                return true;
        }

        cout << "\nThe value entered was an INVALID value!";
        cout << "\nThe Garden Divisions are F-Flowers  V-Vegetable B-Berries";
        cout << "\nWhich do you choose (FVB)? ";
    }

    cout << "\nToo many failed attempts.... exiting program";  
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n(loser)";
    return false;
}

void display_weather(Garden & garden)
{
    cout << "\n\n\n\nGiven the temperature is "<< garden.temperature <<
           " degrees and " << garden.percipitation 
           << " inches of precipitation today.\n";

}

void water_section(Garden & garden, string name, float max_perciptiation)
{
    if(garden.percipitation < max_perciptiation)
    {
        cout << "\nThe "<< name << " in the Garden will be watered.";
    }
    else
    {
        cout << "\nThe "<< name << " in the Garden will NOT be watered.";
    }
}

void water_by_section(Garden & garden)
{
    switch(garden.division)
    {
        case FLOWERS:
            water_section(garden, "Flowers", 0.375);
            break;
        case VEGETABLES:
            water_section(garden, "Vegetables", 0.15);
            break;
        case BERRIES:
            water_section(garden, "Berries", 0.425);
            break;
    }
}

int main()
{
    Garden garden;
    if(read_user_input(garden))
    {
        display_weather(garden);
        if(garden.temperature > 40)
        {
            water_by_section(garden);
        }
        else
        {
            cout << "The berries, vegetables, and flowers in the Garden will NOT be watered today.";
            cout << "\nBecause it is too cold.";
        }
    }

    getchar();
    getchar();

    return 0;
}

Context

StackExchange Code Review Q#5376, answer score: 11

Revisions (0)

No revisions yet.