所以我试图让这个程序允许用户输入他们的数据4次。我希望程序调用每个函数,直到用户第四次输入它们,但它不会让我在第二次尝试时停止。有人可以帮我解决这个问题吗?谢谢!
输出:
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
程序:
#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;
}
答案 0 :(得分:0)
在getData
函数中,不要使用getline
,请执行:
cin >> *cropname;
为什么呢?就像他们在一个类似的问题中所说的那样,“如果你在getline
之后使用cin >> something
,则需要在缓冲区之间刷新换行符。”
答案 1 :(得分:0)
从控制台读取后插入。
cin.clear();
fflush(stdin);
或
cin.flush();
或
cin.ignore(INT_MAX);
取决于您运行它的系统