Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with trying to get the program to display an error message if number

ID: 3849723 • Letter: I

Question

I need help with trying to get the program to display an error message if numberPlants > numberSeeds. I'm not sure how to have it go back to cin >> numberPlants; without it incrementing j, and if I try to decrement it in an if and else statement, it will end up repeating the Category 1: and Season 1: output from before, which is not wanted. Any advice is appreciated. Thank you.

(My code)

#include <iostream> // allows program to perform input and output

#include<iomanip>

using std::cout; // program uses cout

using std::endl; // program uses endl

using std::cin; // program uses cin

int main()

{

int numberSeeds, numberPlants, numberOfCategories, numberOfSeasons, i, j;

double unitPrice, totalSoldPrice, ratio, totalPlants, totalSeeds;;

totalPlants = totalSeeds = 0;

totalSoldPrice = 0;

cout << "Enter the number of the veg category : ";

cin >> numberOfCategories;

cout << "Enter the number of the seasons : ";

cin >> numberOfSeasons;

cout << "------------------------------------------------- ";

for (i = 1; (numberOfCategories != 0) && (i <= numberOfCategories); i++)

for (j = 1; (numberOfSeasons != 0) && (j <= numberOfSeasons); j++)

{

cout << "Category " << i << " : ";

cout << "Season " << j << " : ";

cout << "Enter Number of Seeds : ";

cin >> numberSeeds;

cout << "Enter Number of Plants : ";

cin >> numberPlants;

cout << "Enter UnitPrice : ";

cin >> unitPrice;

totalSeeds += numberSeeds;

totalPlants += numberPlants;

totalSoldPrice += (numberPlants * unitPrice);

ratio = (totalPlants / totalSeeds) * 100;

//cout << "Error. Amount of plants cannot exceed number of seeds. Try again. ";

if (numberOfSeasons == j)

{

cout << "Growing ratio for category " << i << " : " << std::setprecision(4) << ratio << "% "

<< "Total sold price for category " << i << " : " << std::setprecision(5) << totalSoldPrice << endl

<< "------------------------------------------------- ";

}

}

if (numberOfCategories == 0 && numberOfSeasons == 0)

cout << "Zero categories and seasons entered. ";

else if (numberOfCategories == 0 || numberOfSeasons == 0)

cout << "Either zero categories or seasons entered. ";

return 0;

}

Explanation / Answer

i hope this is what you are looking for

#include <iostream> // allows program to perform input and output

#include<iomanip>

using std::cout; // program uses cout

using std::endl; // program uses endl

using std::cin; // program uses cin

int main()

{

int numberSeeds, numberPlants, numberOfCategories, numberOfSeasons, i, j,k=0;

double unitPrice, totalSoldPrice, ratio, totalPlants, totalSeeds;;

totalPlants = totalSeeds = 0;

totalSoldPrice = 0;

cout << "Enter the number of the veg category : ";

cin >> numberOfCategories;

cout << "Enter the number of the seasons : ";

cin >> numberOfSeasons;

cout << "------------------------------------------------- ";

for (i = 1; (numberOfCategories != 0) && (i <= numberOfCategories); i++)

for (j = 1; (numberOfSeasons != 0) && (j <= numberOfSeasons); j++)

{

cout << "Category " << i << " : ";

cout << "Season " << j << " : ";

cout << "Enter Number of Seeds : ";

cin >> numberSeeds;

while(k==0)

{

cout << "Enter Number of Plants : ";

cin >> numberPlants;

if(numberPlants>numberSeeds)//if thiscondition is true error message will be displayed and k will not be //changed, therefore the loop will run again to take input numberPlants.

{

cout << "Error. Amount of plants cannot exceed number of seeds. Try again. ";

}

else//if condition is false k becomes 1 and execution gets out of while loop

k=1;

}

cout << "Enter UnitPrice : ";

cin >> unitPrice;

totalSeeds += numberSeeds;

totalPlants += numberPlants;

totalSoldPrice += (numberPlants * unitPrice);

ratio = (totalPlants / totalSeeds) * 100;

//cout << "Error. Amount of plants cannot exceed number of seeds. Try again. ";

if (numberOfSeasons == j)

{

cout << "Growing ratio for category " << i << " : " << std::setprecision(4) << ratio << "% "

<< "Total sold price for category " << i << " : " << std::setprecision(5) << totalSoldPrice << endl

<< "------------------------------------------------- ";

}

}

if (numberOfCategories == 0 && numberOfSeasons == 0)

cout << "Zero categories and seasons entered. ";

else if (numberOfCategories == 0 || numberOfSeasons == 0)

cout << "Either zero categories or seasons entered. ";

return 0;

}