Help with looping a set of code

Hello, this is a code snippet of something that I need help with. So, essentially, I need to loop each one of these if statements n times. The user inputs which plant they want and how many of each plant. This current code only works with two of the plants and not very well. I know I'm close but I can't crack the right code. The code found after each plant needs to loop the amount of times the user wants to choose for the specific plant. For example, the user chooses a peach, then the code should output the peach code four times. Each peach plant is 1x1 in size so the total amount of size should output to four since the user chose just four plants. However, if the user chose two peaches and two tomatoes then the two peaches would add to a total of 2 square feet and the two tomatoes would add to
4 so, in total, the complete size would be six. Or, if the user chose one of each plant, the total size should be 8. Apple and peach are both 1 in size, mango is 4 in size, and tomato is 2 in size. I need the code to work witn a variety of user input but I can't figure that out. I'm thinking about using a while loop but I'm not sure if that will work either. I'm sorry if I couldn't phrase my problem correctly. This code is currently messy because I haven't optimized it yet since it doesn't work. I will reply ASAP if you have a question. I greatly appreciate any and all help!

Thank you!

[code]


int Plants::plantinput(int& size) {
int n{}, i, s{}, iP{}, iT{}, iA{}, iM{}, input_size{}, input_size2{}, input_size3{}, input_size4{}, psize, tsize, asize, msize;
string input, Peach, Tomato, Apple, Mango;

size == 0;
cin >> n;

for (n = n; n > 0; n--)
{
if (n > 0) {
cout << "Now input what plant you would like to harvest below: " << "\n";
cin >> input;



if (input == "Peach") {
cout << "Size: 1x1" << endl;
cout << "How many peaches would you like to plant? ";
cin >> iP;

input_size = iP;
psize = 1 * iP;
size = size + psize;


if (n > 0) {
cout << "Now input what plant you would like to harvest below: " << "\n";
cin >> input;
}
else {
break;
}

n = n - iP;
}

if (input == "Tomato") {
cout << "Size: 2x2" << endl;
cout << "How many tomatoes would you like to plant? ";
cin >> iT;

input_size2 = 2 * iT;
tsize = 2 * iT;
size = size + tsize;

if (n > 0) {
cout << "Now input what plant you would like to harvest below: " << "\n";
cin >> input;
}
else {
break;
}


n = n - iT;
}

}

psize= 1* iP;
tsize= 2* iT;
size = psize + tsize;
}

cout << "You have chosen " << size << " square feet of plants to harvest " << "\n" << endl;
return 0;
}
This is not meant to be a definitive answer but shows how to overcome all the ifs by using parallel arrays. There is ample scope here to improve the info displayed and error check/prompt for plants not in the list.

By the look of your samples you don't need to consider the knapsack problem red-herring.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

const int PEACH = 1;
const int MANGO = 4;
const int TOMATO = 2;
const int APPLE = 1;

int main()
{
    const int plant_size[]{PEACH, MANGO,TOMATO,APPLE};
    const string plant_name[]{"peach", "mango", "tomato", "apple"};
    int NO_PLANTS = sizeof(plant_name)/sizeof(string);
    
    string input;
    int quantity{0};
    int area{0};
    
    while(
          cout << "What plant? " &&
          cin >> input &&
          input != "q"
          )
    {
        cout << "How many? ";
        cin >> quantity;
        
        int location{-1};
        for(int i = 0; i < NO_PLANTS; i++)
        {
            if(plant_name[i] == input)
            {
                location = i;
                area += quantity * plant_size[i];
                break;
            }
        }
        cout << "So far, you have chosen " << area << " sq ft to harvest\n";
        
    }
    
    return 0;
}


What plant? peach
How many? 2
So far, you have chosen 2 sq ft to harvest
What plant? tomato
How many? 2
So far, you have chosen 6 sq ft to harvest
What plant? q
Program ended with exit code: 0
Last edited on
PS Of course if it is an instance of the knapsack problem then here's a place to start ..
https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
Hello theforgottenone4,

As I look over what you have the first question I have is what is "n" and what kind of a value should it have? A proper name would help to understand this, but for now some idea of what value to give "n" would help.

Just so you know for (n = n; n > 0; n--) the first has no real effect. "n" is defined outside the for loop and setting it equal to its-self does nothing. It is the same as saying for (; n > 0; n--) which would work the same.

I believe the first if statement in the for loop will always be true and that, with the rest of your code, the for loop will end before the first if statement ever becomes false.

I have a hard time understanding why you would need for loops because if you use what you have more efficiently I think it should work, but that still comes back to what "n" is for.

This code may need some changes to deal with "n" if needed, but should give you an idea what you can do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// <--- Most comon includes.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

void plantInput(int& size);

int main()
{
	int size{};

	plantInput(size);


	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

void plantInput(int& size)
{
	constexpr int PEACH{ 1 }, TOMATO{ 2 }, APPLE{ 1 }, MANGO{ 4 };

	//int n{ 3 }, i, s{}, iP{}, iT{}, iA{}, iM{}, input_size{}, input_size2{}, input_size3{}, input_size4{}, psize, tsize, asize, msize;
	int n{ 3 }, input_size{};
	std::string input{ "peach" }; // , Peach, Tomato, Apple, Mango;

	size = 0;  // <--- Changed.

	std::cout << "\n The available plants are: (Peach, Tomato, Apple and Mango)\n";  // <--- Changed.
	//std::cout << "\n How many plants would you like to use?: ";
	//std::cin >> n;

	for (int lc = n; lc > 0; lc--)
	{
		std::cout << "\n Now input what plant you would like to harvest: ";  // <--- Changed.
		std::cin >> input;

		if (input == "Peach" || input == "peach")  // <--- Changed.
		{
			std::cout << "\n Size: 1 x 1 ";
			std::cout << "How many peaches would you like to plant? ";
			std::cin >> input_size;

			//input_size = iP; // <--- input_size is never used after this point right now.
			//psize = 1 * iP;
			//size += psize;
			size += PEACH * input_size;

			//n = n - input_size; // <--- Not sure what to do with this?
		}

		if (input == "Tomato" || input == "tomato")  // <--- Changed.
		{
			std::cout << "\n Size: 2 x 2 ";
			std::cout << "How many peaches would you like to plant? ";
			std::cin >> input_size;

			//input_size = iT; // <--- input_size is never used after this point right now.
			//psize = 1 * iT;
			//size += psize;
			size += TOMATO * input_size;

			//n = n - input_size; // <--- Not sure what to do with this?
		}
	}

	std::cout << "\n You have chosen " << size << " square feet of plants to harvest " << "\n" << std::endl;
}

Without a better understanding of the whole program this seems to accomplish what I think you are wanting. Or at least maybe a better start. If not let me know.

The variables "input" and "input_size" can be reused because each time through the for loop they will be receiving different values for that iteration and change on the next.

Andy
Thanks for the help! I'm going to try both of these methods later and update you. N is just the input for how many plants the user wants. For example, the program starts and it asks the user how many plants they want and then which plant they want. For each plant chosen and how many chosen, the value of N needs to decrease by one. As N decreases, the size of each plant needs to increase. So it a user chooses to plant 5 total plants, they can then choose a Peach. They should then be able to choose three peaches. Then they can choose one mango and one tomato. In this situation, N is 5 so this value needs to decrease to 0 for the loop to stop. The five plants chosen in this situation should add to 9 so the output should be 9 square feet. Later on in the program I have some more code to sort these plants that utilize the variable input_size so that's why this code is present without having any perceived use.

What I was trying to do is just have the code change the value of N inside the for loop but I couldn't get that to work.
Last edited on
Hello theforgottenone4,

Now that I know better I will see what I can come up with.

Andy
Hello theforgottenone4,

If I am understanding correctly I believe this may be what you are wanting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (int lc = numOfPlants; lc > 0 && numOfPlants > 0; lc--)
{
	std::cout << "\n Now input what plant you would like to harvest: ";  // <--- Changed.
	std::cin >> input;

	if (input == "Peach" || input == "peach")
	{
		std::cout << "\n Size: 1 x 1 ";
		std::cout << "How many peaches would you like to plant? ";
		std::cin >> input_size;

		size += PEACH * input_size;

		numOfPlants -= input_size;  // <--- Changed.
	}
	else if (input == "Tomato" || input == "tomato")

Line 14 would need to be duplicated in each "else if". Using the "else if"s you could skip 3 or 2 or 1 that are not needed to be checked. Of course the program would work just as well with individual if statements.

I meant to mention this, but got side tracked. Not having more of the program to work with I created the program just to work with the one function. Once you get what you want you can change the class function with what you finally come up with. Just easier to work with one function and not the whole program.

Andy
Hello, I'm so sorry. I suddenly got very busy and haven't had time to work on the program whatsoever. I'm finally able to work on it now and I'm going to try your ideas. I believe the line 14 code in the above reply is what I am missing but there's only one way to test it. Thank you so much for the help!
I got it! Thanks so much, Andy! Your code is working perfectly and I'm on track to finish the program. Greatly appreciated! You saved me hours of time!
Topic archived. No new replies allowed.