2012-08-07 74 views
0

這裏是我的主:異常處理麻煩,爲什麼?

int main() { 
    Inventory Master; 
    bool flag; 
    Customer Bob("Bob", "CreditCard.txt"); 
    Customer Joe("Joe", "CreditCard.txt"); 



    Master.firststock("inventory.txt"); 
    vector<Food> temp = Master._Inv; 
    cout <<"Hi, What would you like to buy today?" << endl; 
    for(unsigned int i=0; i<temp.size(); i++) { 
    cout << temp[i].name << " " << temp[i].quant << " " << temp[i].price << endl; 
    } 

    cout <<"\n"; 
    Food Apple("Apples", .99, 10); 
    Food Orange("Oranges", .99, 10); 
    Food Chip("Chips", 3.00, 10); 

    cout <<"\nHi Bob" << endl; 
    flag = Bob.addCart(Apple, 7, &Master); 
    cout <<"Bob's total purchases are Currently: \n"; 
    Bob.report(); 
    flag = Bob.addCart(Orange, 2, &Master); 
    flag = Bob.addCart(Chip, 2, &Master); 
    Bob.report(); 
    flag = Bob.removeCart(); 
    Bob.report(); 
    cout <<"Bob, "; 
    flag = Bob.checkout(&Master); 

這裏我實現了從我的矢量_cart去除食物如下:

bool Customer::removeCart() { 
    bool flag; 
    int q = 0; 
    unsigned int i=0; 
    string remove; 

    cout << "\nWhat would you like to remove and how much would you like to remove?" << endl; 
    cin >> remove >> q; 
for (i =0; i < _Cart.size(); i++) { 
    if(remove == _Cart[i].name) { 
     if (q >= 0) { 
    _Cart[i].quant -= q; 
    //inv->_Inv[i].quant += q; 
    cout <<"\nYou removed " << q << " " << remove <<" In your cart\n" << endl; 
    return true; 
     } 
     if (q < 0) { 
      cout << "Invalid number of " << remove << " being removed.\n" << endl; 
      return true; 
     } 
    } 
    else {  
    try { 
    throw remove; 
} 

    catch (string param) { 
    cout << "\n" << remove << " doesn't exist in your cart\n" << endl; 
     } 

     return true; 
    } 
} 

我包含的功能removeCart頭:

class Customer { 
    public: 

    Customer(string n, string fileName); 
    ~Customer() { _Cart.clear(); }; 
    bool addCart(Food f, int q, Inventory* inv); 
    bool removeCart(); 
    void report(); 
    bool checkout(Inventory* inv); 
    protected: 
    string remove; 
    string name; 
    int q; 
    int card; 
    double balance; 
    CreditCard _CC(int card,double balance); 
    vector<Food> _Cart; 
}; 

現在由於某種原因,當我打電話給removeCart,輸入「蘋果」的作品,但我注意到我做了一個叫蘋果的食物對象,所以不知道爲什麼打字「蘋果」是爲r emoved而不是「Apple」。此外,當我嘗試「橙色」或「芯片」的例外顯示,但正如你可以看到在主我添加芯片和橙色鮑勃的車。我可以欣賞幫助。

+0

發佈例外消息並顯示其發生的行。當你輸入_'Apple'_時會發生什麼? – Aesthete 2012-08-07 04:12:35

+0

當我輸入控制檯'蘋果3'我得到的例外:蘋果不存在你的購物車 – tensuka 2012-08-07 04:34:52

回答

0

您正在創建一個名爲Apple的對象,其中包含std :: string類型的成員,其中包含字符「Apple」。只有你的編譯器知道你調用了一個對象Apple,但你的程序比較了字符串「Apple」和你的輸入。橙和芯片一樣。

0

宣佈對象在你的代碼中調用Apple地方。

你再實例Apple類的實例和Apple::name成員設置爲'Apples',一個字符串。

你不與類名比較輸入,你的輸入與的Apple類的會員數據進行比較。