0

So I am trying to get this program to allow the user to input their data 4 times. I want the program to call each function until the user enters them for the 4th time, but it is not letting me and just stops at the 2nd attempt. Can someone help me with this problem? Thanks!

Output:

Enter the crop name:
Corn
Enter cost, yield, price per bushel, and increase data:
5
5
5
5
           Minimum Profit      Maximum Profit      Average Profit
Corn    $20000.00         $145000.00         $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
Peas
           Minimum Profit      Maximum Profit      Average Profit
    $20000.00         $145000.00         $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
           Minimum Profit      Maximum Profit      Average Profit
    $20000.00         $145000.00         $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
           Minimum Profit      Maximum Profit      Average Profit
    $20000.00         $145000.00         $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
           Minimum Profit      Maximum Profit      Average Profit
    $20000.00         $145000.00         $82500.00
Press any key to continue . . .

enter code here

Program:

#include <iostream>
#include<iomanip>
#include<string>

#define ACRES 1000; 

using namespace std;

//Function prototypes.
void getData(string*, float*, int*, float*, float*);
void calculate(float, int, float, float, float*, float*, float*);
void printResults(string, float, float, float);

int main()
{
    string name;
    float CostPerAcre, increase, bushel, MinNP, MaxNP, AvgProfit2, bestcrop;
    int yield;
    for(int i = 0; i <= 4; ++i)
    {
    getData(&name, &CostPerAcre, &yield, &bushel, &increase);
    calculate(CostPerAcre, yield, bushel, increase, &MinNP, &MaxNP, &AvgProfit2);
    printResults(name, MinNP, MaxNP, AvgProfit2);
    }

    //cout << endl << "Old MacDonald, you should plant " << bestCrop << endl << endl;
    return 0;
}

// This function allows the user to input their data.
void getData(string *cropname, float *costpa, int *bpa, float *dpb, float *dpbincrease)
{
        cout << "Enter the crop name: " << endl;
        getline(cin, *cropname);
        cout << "Enter cost, yield, price per bushel, and increase data: " << endl;
        cin >> *costpa >> *bpa >> *dpb >> *dpbincrease;
}

// This function uses the data to calculate the projected range of net profit and the average net profit for each crop.
void calculate(float costpa, int bpa, float dpb, float dpbincrease, float *MnNP, float *MxNP, float *AvgProfit)
{
    float MnGP, MxGP;
    float costofCrop = costpa * ACRES;
    MnGP = (bpa * dpb ) * ACRES;
    MxGP = MnGP + (MnGP * dpbincrease);
    *MnNP = (MnGP)-costofCrop;
    *MxNP = (MxGP)-costofCrop;
    *AvgProfit = (*MxNP + *MnNP) / 2;
}

// This function displays the minimum profit, maximum profit, and average profit.
void printResults(string cropname, float MnNP, float MxNP, float AvgProfit)
{
    cout << setw(25) << right << "Minimum Profit" << setw(20) << right << "Maximum Profit" << setw(20) << right << "Average Profit" << endl;
    cout << cropname << setw(5) << right << setprecision(2) << fixed << '$' << MnNP << setw(10) << right << setprecision(2) << fixed << '$' << MxNP << setw(10) << right << setprecision(2) << fixed << '$' << AvgProfit << endl;
}
TheEWL
  • 19
  • 1
  • 6
  • 1
    Note that your loop would run 5 times, (if not for your other bug), 0,1,2,3,4. Fencepost error:( – Martin James Feb 25 '16 at 21:27
  • Possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Barmar Feb 25 '16 at 21:42

2 Answers2

0

In the getData function, instead of using getline, do:

cin >> *cropname;

Why? Like they said in a similar question, "If you're using getline after cin >> something, you need to flush the newline out of the buffer in between".

dario_ramos
  • 7,118
  • 9
  • 61
  • 108
0

insert that after reading from console.

cin.clear();
fflush(stdin);

or

cin.flush();

or

cin.ignore(INT_MAX);

depends on the system you are running it

Check This for more Info

Community
  • 1
  • 1
Manu
  • 182
  • 2
  • 7