Description You bought several acres of farm to grow and sell vegetables. Suppos
ID: 3916719 • Letter: D
Question
Description You bought several acres of farm to grow and sell vegetables. Suppose that you want to grow a maximum of two types of vegetables. Write a program that prompts the user to do the following 1. Enter the total farm area in acres (double) 2. The number of vegetables (one or two) that the user wants to grow (int) 3. If the user wants to grow two types of vegetables, then specify the portion, as a percentage, of the farm land used for each type of vegetable (read two int variables) 4. Enter the seed cost, plantation cost, fertilization cost, labor cost, for each acre (all double values) 5. Enter vegetable selling price per acre 6. Output the cost to produce vegetables 7. Output the profit/loss Specifications In order to get full credit, your code must contain/perform the following Variables with correct type with meaningful names Comment your code, comment the variables and parts of code for input, output, and calculations Program must compile and run with any input Once again, use iomanip to format the output (two decimal places and aligned properly) Check for input failure and use exit(0) to terminate your program whenever you have any failure, you need to include cstdlib in order to use this function Input failure is if a non number is entered or if a non positive number is entered If the number of vegetables is not 1 or 2 you will also need to terminate the program If two vegetables are entered, and if the two percentages read do not sum up to 100, terminate the programExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
int getInt(){
int d;
if(! (cin >> d) || d <= 0)
{
cout << "Invalid input" << endl;
exit(1);
}
return d;
}
double getDouble(){
double d;
if(!(cin >> d) || d <= 0)
{
cout << "Invalid input" << endl;
exit(1);
}
return d;
}
int main(){
double farmArea ;
int numVeg;
int veg1Perc = 0, veg2Perc = 0;
double perc1 = 0, perc2 = 0;
double seed = 0, plantation = 0, fertilization = 0, labor = 0;
double selling1 = 0, selling2 = 0;
double productionCost, sellingPrice, profit;
cout << "Enter area of the farm: ";
farmArea = getDouble();
cout << "Enter the number of vegetables: ";
numVeg = getInt();
if(numVeg > 2){
cout << "Invalid input" << endl;
exit(1);
}
if(numVeg == 2)
{
cout << "Enter percentage of farm used for vegetable 1: ";
veg1Perc = getInt();
cout << "Enter percentage of farm used for vegetable 2: ";
veg2Perc = getInt();
if(veg1Perc + veg2Perc != 100){
cout << "Percentages do not add up to 100" << endl;
exit(1);
}
cout << "Enter seed cost, plantation cost, fertilization cost and labor cost for each acre: "<< endl;
seed = getDouble();
plantation = getDouble();
fertilization = getDouble();
labor = getDouble();
cout << "Enter selling price per acre for vegetable 1 and 2: ";
selling1 = getDouble();
selling2 = getDouble();
perc1 = veg1Perc / 100.0;
perc2 = veg2Perc / 100.0;
sellingPrice = perc1 * farmArea * selling1 + perc2 * farmArea * selling2;
}
else{
cout << "Enter seed cost, plantation cost, fertilization cost and labor cost for each acre: "<< endl;
seed = getDouble();
plantation = getDouble();
fertilization = getDouble();
labor = getDouble();
cout << "Enter selling price per acre: ";
selling1 = getDouble();
sellingPrice = farmArea * selling1;
}
productionCost = farmArea * (seed + plantation + fertilization + labor);
profit = sellingPrice - productionCost;
cout << fixed << setprecision(2);
cout << left << setw(20) << setfill('.') << "Production cost" << "$" << setfill(' ') << right << setw(10) << productionCost << endl;
cout << left << setw(20) << setfill('.') << "Selling price" << "$" << setfill(' ') << right << setw(10) << sellingPrice << endl;
if(profit >= 0)
cout << left << setw(20) << setfill('.') << "Profit" << "$" << setfill(' ') << right << setw(10) << profit << endl;
else
cout << left << setw(20) << setfill('.') << "Loss" << "$" << setfill(' ') << right << setw(10) << -profit << endl;
}
output
-----
Enter area of the farm: 1500
Enter the number of vegetables: 1
Enter seed cost, plantation cost, fertilization cost and labor cost for each acre:
1.99 2.41 0.55 2.99
Enter selling price per acre: 5.67
Production cost.....$ 11910.00
Selling price.......$ 8505.00
Loss................$ 3405.00
Enter area of the farm: 500
Enter the number of vegetables: 2
Enter percentage of farm used for vegetable 1: 30
Enter percentage of farm used for vegetable 2: 70
Enter seed cost, plantation cost, fertilization cost and labor cost for each acre:
0.15 0.25 0.34 0.87
Enter selling price per acre for vegetable 1 and 2: 12.67 11.45
Production cost.....$ 805.00
Selling price.......$ 5908.00
Profit..............$ 5103.00