2016-02-25 52 views
0

所以我试图让这个程序允许用户输入他们的数据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; 
} 
+1

请注意,你的循环将运行5次,(如果不适合你的其他b ug),0,1,2,3,4。 Fencepost错误:( –

+0

[cin和getline跳过输入]的可能重复(http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Barmar

回答

0

而是采用getlinegetData功能,这样做:

cin >> *cropname; 

为什么?就像他们在一个类似的问题中所说的那样,“如果在cin >> something之后使用getline,则需要将新行从两者之间的缓冲区中清除”。

0

从控制台读取后插入。

cin.clear(); 
fflush(stdin); 

cin.flush(); 

cin.ignore(INT_MAX); 

取决于系统正在运行它

Check This for more Info