2013-11-21 66 views
-3

我的程序有問題。我的代碼可以運行,但導致它停止工作。我已經多次查看了該程序,但是看不到問題所在。感謝任何幫助我的人。真的很感激。程序無法正常工作的解決方案

#include <iostream> 
#include <string> 

using namespace std; 

double getdiscount(int customer_city, double gal_last) 

{ 
    getdiscount(customer_city, gal_last); 
    return 0; 
} 


int main() 
{ 
    int customer_city ; 
    double gal_last, gal_bought, price_per_gal, discount, tot_cost; 

    cout << " The number of gallon being purchased in gal: " << endl; 
    cin >> gal_bought; 

    cout << " The price per gallon you bought : "<<"RM"; 
    cin >> price_per_gal; 

    cout<<" Enter customer city : "<<endl; 
    cin>>customer_city; 

    cout<<" Number of gallons of oil used last year : " <<endl; 
    cin>>gal_last; 

    switch (customer_city) 

    { 
    case 1: 
     (customer_city == 1); 
     if (gal_last>=1000) 
      discount = 0.9; 
     else 
      discount = 1; 
     break; 

    case 2: 
     (customer_city == 2); 
     (gal_last >=1200); 
     discount = 0.9; 
     break; 

    default: 
     if (gal_last >=1500) 
      discount = 0.9; 

     cout<< " Discount rate is : "<<discount<<endl; 
    } 

    tot_cost = gal_bought* (price_per_gal*getdiscount(customer_city, gal_last)); 

    cout<< " The total cost : "<<tot_cost<<endl; 

    return 0; 
} //end main function 
+4

調試和看到這行失敗] – khajvah

+1

不說getdiscount功能只是進入一個遞歸的無限循環? – fjc

+1

看看你的'getdiscount()'函數。它做的唯一工作是遞歸調用自己。請完成寫你的程序,然後回來。 – Blastfurnace

回答

4

這是遞歸調用本身......

double getdiscount(int customer_city, double gal_last) 

{ 
    getdiscount(customer_city, gal_last); 
    return 0; 
} 

,直到你得到一個計算器...

+0

是的,這是答案。但這不是真的*正確*答案。給一個男人一條魚...... – adelphus

+0

@adelphus,在這種情況下,這是非常簡單的監督,有時另一雙眼睛只是做伎倆...... – Nim

相關問題