void displayTotals(int num, double priceBushel[], double bushel, double subtotal, double grandTotal, const double LOSS_PERCENT)
{
if (num >= 0 && num <= 4)
{
subtotal = priceBushel[num] * bushel;
cout << "Subtotal: " << subtotal << endl;
grandTotal = subtotal * LOSS_PERCENT;
cout << "Grand Total: " << grandTotal << endl;
}
else if (num < 0 || num > 4)
cout << "The number you entered does not correspond to any crop in this program." << endl;
}
Basically, this program should take the number you entered and use it to plug the array element corresponding to your number into the equations listed.
Normally, I would do this a little differently.. however, I have to follow the guidelines given by my instructor. Also, I need to find a place to insert a repetition structure.
The guidelines are:
Appropriate comments and directives (will add comments later)
Appropriate variables (at least 1 constant variable and string variable)
1 Selection
1 Repetition
1 Value-Returning Function or Void Function
1 One-Dimensional Arrays
Output that include a subtotal and grand total
The program description is(rough description before program was begun):
My program is going to be a farmers calculator, so to say. I found it to be an appropriate topic due to the vast farming community that resides in North Carolina, especially around Wilson. I have actually grown up on a farm myself and have accustomed myself to some of the responsibilities that a farmer must maintain, this could be a tool that would improve and/or cut down on wasted time. My possible input would be the pounds of crop grown, I will specify the crop restrictions(corn, tobacco, cotton, green beans, etc). The processing would be matching the crop with it's specified price per pound. The outputs would be total amount and subtotal, subtotal being the amount prior to non-salvageable crop deductions. What I mean by "non-salvageable" is crops that are either rotten or unfit for consumer purchase, which can add up to quite a bit in a rough season. I feel as though my topic would be more practical for my surroundings and would assist a larger audience here in North Carolina.
Ah, good point. Well the question would be, why isn't it working properly. There seems to be a problem with the priceBushel[num] array... the program is using the actual number I enter within the equations rather than using the element in the array corresponding to the number. For example, I'll input the number 2 for the element 24.00 in my array, however it'll just use "2" in the equations.
Thanks! I was just having problems using the program idea and incorporating everything that she asked for...I feel like the program is getting extremely jumbled, it's quite irritating. Do you see any different route to accomplish the program while still staying within the guidelines? I don't need an entire page of code, just a few keywords. Like which repetition structure you'd use or which constant variable etc.
- An array for desired quantities (meets 1D array requirement)
- An array to store the prices (As you already have)
- Integers for user input (index and amount)
- A double to store subtotal
- Initialise quantities array to zero
- Start while loop (meets repetition requirement)
- Ask user for crop [1 - 4] (meets selection requirement)
- Ask user for amount
- Add the amount entered to the corresponding quantity index
- End while loop when choice = -1 or some other arbitrary value
- Create a double returning function that loops through the quantities array builds up a subtotal based on the quantities and values.
Return that amount to the subtotal double declared earlier (meets function requirement)
- Output the subtotal (meets output requirement)
The only things I haven't catered for are the constant and string variables. Make whatever you want constant. The percent that you're currently doing is a good bet. I suppose you could make the crop prices constant you'd maybe need to change the processing I've outlined about.
The string variable I take a bit of issue with as it's not really "appropriate". Just to satisfy criteria, I'd do something like you're already doing with the keys. Or, simpler, just create a string called welcomeMessage and print it at the top of the program.
I think, in general, a better approach to this would be using structs or classes. Given that they're not mentioned in the brief, I'm assuming you haven't studied them left and, as such, have left them out of this suggestion.
I'm a bit confused by what values are to be added to the "desired quantities" array.
- An array for desired quantities (meets 1D array requirement)
- Ask user for amount
- Add the amount entered to the corresponding quantity index
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string welcomeMessage = "Tobacco crops = 0, Sweet Potato = 1, Cotton = 2, Corn = 3";
int num = 0;
int bushel = 0;
int quantities[] = {0};
const double LOSS_PERCENT = .08;
double priceBushel[4] = {67.32, 10.00, 24.00, 7.37};
double subtotal = 0.0;
while (num != -1)
{
cout << "Enter a number corresponding to your crop(-1 to stop): ";
cin >> num;
cout << "Enter the number of bushels per acre: ";
cin >> bushel;
quantities[1] += bushel;
}
void displayTotals(int num, double priceBushel[], double bushel, double subtotal, double grandTotal, const double LOSS_PERCENT, double deductions)
{
if (num >= 0 && num <= 4)
{
subtotal = priceBushel[num] * bushel;
cout << "Subtotal: " << subtotal << endl;
deductions = (subtotal * LOSS_PERCENT);
grandTotal = subtotal - deductions;
cout << "Grand Total: " << grandTotal << endl;
}
else if (num < 0 || num > 4)
cout << "Error: Number must be between 1 and 4." << endl;
}
This is my original program... it's working but I'm still having an issue as to where to insert the while loop. Also, I tried inserting a while loop but it would ask for the bushel amount again even after inputting -1 into the number amount. Same goes for the if else statement in my void definitions, if I input 5 or a number greater than 4 or less than 0, it will still ask for the bushel amount before popping the error message up.
I apologize for all the questions! I'm currently taking a community college course and my teacher isn't exactly the greatest... she refuses to answer any questions regarding our programming and will only outline the powerpoints for us.(which don't give any relevant examples)
Use the code tags, otherwise it's difficult to read your code.
In line 33, I think your if condition should be num < 4, because 4 isn't an option unless I'm mistaken. That's not related to your problem just something I noticed.
I think you might be better off using a do while loop for your displayTotals function. Are you familiar with those? In your case, your while condition would be
while (num != -1);
If you choose to stick with a while loop, you need to make sure you have a cin >> outside and above the while loop, and inside close to the bottom of the loop. After looking at your code again, it doesn't look like you do this.
However, one thing I'm sure of is that you aren't updating the condition of the while loop somehow, which causes it to reiterate. Does any of that help?
Thanks everyone, and sorry about the code tags (or lack thereof). I was able to figure out the problems with my program and have since fixed it. :) Marking as solved.