2016-02-25 91 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