這裏是我的主:搬運車中的食物,但某些食物沒有清除?爲什麼
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,輸入「蘋果」的作品,但我注意到我做了一個名爲蘋果的食物對象,所以不知道爲什麼打字「蘋果」適用於刪除而不是「蘋果」。此外,當我嘗試「橙色」或「芯片」的例外顯示,但正如你可以看到在主我添加芯片和橙色鮑勃的車。我甚至嘗試過「橙子」和「芯片」,它仍然讓我感到異常而不是工作。我可以欣賞幫助。
你的源碼不是一個完整的程序,所以,你正在使用'std :: string'和'std :: vector',對嗎?我只是問,因爲我沒有看到'#include'使用'namespace',所以它可以是任何東西。 – 2012-08-07 05:54:37
我的意思是我可以包括整個程序,如果你願意的話。我只是因爲某些原因提供了一切,但如果需要的話我可以。我可以嗎? – tensuka 2012-08-07 06:12:35
您需要包含* less *代碼。只是一個小的,可編輯的樣本,再現你的問題。 – juanchopanza 2012-08-07 06:17:05