异常处理麻烦,为什么?

问题描述:

这里是我的主:异常处理麻烦,为什么?

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

您正在创建一个名为Apple的对象,其中包含std :: string类型的成员,其中包含字符“Apple”。只有你的编译器知道你调用了一个对象Apple,但你的程序比较了字符串“Apple”和你的输入。橙和芯片一样。

宣布对象在你的代码中调用Apple地方。

你再实例Apple类的实例和Apple::name成员设置为'Apples',一个字符串。

你不与类名比较输入,你的输入与的Apple类的会员数据进行比较。