c++ - Push.back into vector behavior maybe or something wrong with my for statement?

标签 c++ visual-c++

    bool Customer::checkout(Inventory* inv) {

  double total = 0;

  for( unsigned int i=0; i < _Cart.size(); i++ ) {
      total += (_Cart[i].price * _Cart[i].quant); //

  }

  if( balance < total ) {
      cout << "Checkout Failed. You do not have enough money to afford everything." 
          <<"Please go back and remove items as necessary.\n";
      return false;
  }

  else {

    unsigned int j = 0;                                          //Then, you need to add said food into the purchased vector
    for (j = 0; j < inv->_Purchases.size(); j++)    {                //When you add the food into the purchased vector, you need to look through
        if (inv->_Purchases[j].name == _Cart[j].name) {     //the entire purchased vector to see if the food is already there, 
             inv->_Purchases[j].quant += _Cart[j].quant;     //if so, increment quantity if not, just push the food into the vector
             break;

        }
    }

         if( j == inv->_Purchases.size()) {
             inv->_Purchases.push_back(_Cart[j]);
             cout << "Checkout is Complete.\n";
             return true;
        }

        _Cart.clear();

  }



    balance -= total;
    inv->interval += 1;
    inv->restock( "restock fruits.txt", 2 );
    inv->restock( "restock inventory.txt", 3);
    cout << "Checkout Complete.\n";
    return true;

}



    void Inventory::summary() {
    double total = 0;
    for( unsigned int j=0; j<_Purchases.size(); j++ ) {
      cout << "\nTotal purchases for the store are:";
      cout << "\nFood: " << _Purchases[j].name << " | Quantity: " << _Purchases[j].quant << " | Price: " << _Purchases[j].price << endl;
      total += (_Purchases[j].quant * _Purchases[j].price);
  }
  cout << "Total Purchase: " << total << endl;

    //cout the purchased vector's .name
    //cout the quant 
    //cout the price*quant
    //make a total, and cout it at the end
    }

这是我的主要内容:

    #include "foodservice.h"
    #include <iostream>
    using namespace std;

    int main() {
      Inventory Master;
      bool flag;
      Customer Bob("Bob", 12345, 100.00 );
      Customer Joe("Joe", 56789, 50.00 );
      Customer Arjun("Arjun", 98765, 35.89 );
      Customer Randy("Randy", 54689, 30.28);
      Customer Mark("Mark", 76598, 15.18);


  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 Oranges("Oranges", .99, 2);
  Food Chips("Chips", 3.00, 2);

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

  cout <<"\nHi Arjun" << endl;
  flag = Arjun.addCart(Apple, 3, &Master);
  cout <<"Arjun, ";
  Arjun.report();
  flag = Arjun.checkout(&Master);

  Master.summary();

当我调用 summary();总的来说,出于某种原因,我似乎只让苹果显示输出,如下所示:

“商店的总采购量是: 食物:苹果 |数量: 7 |价格:0.99 总购买量:6.93"

但如您所见,我已将苹果、橙子和薯条都添加到 Bob 的购物车中,因此这三个应该都显示,但实际上没有。我将不胜感激。这项工作仍在进行中。感觉跟有关系

if( j == inv->_Purchases.size()) {
             inv->_Purchases.push_back(_Cart[j]);
             cout << "Checkout is Complete.\n";
             return true;

But i am not sure.

最佳答案

问题出在您添加已购买商品的循环中。逻辑错误。

你写的

for (j = 0; j < inv->_Purchases.size(); j++)
    if (inv->_Purchases[j].name == _Cart[j].name)
...

但这比较了 _Purchases[0]_Cart[0]_Purchases[1]_Cart[1] 等。您需要两个循环,以便将每个 _Cart 项目与每个 _Purchases 项目进行比较。像这样

for (i = 0; i < _Cart.size(); i++)
{
    for (j = 0; j < inv->_Purchases.size(); j++)
    {
        if (_Cart[i].name == inv->_Purchases[j].name)
...

剩下的就交给你了。这将是一个很好的练习。

关于c++ - Push.back into vector behavior maybe or something wrong with my for statement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814798/

相关文章:

c++ - vector 中的段错误

c++ - 无法在 Windows 上为 BlackBerry 10 构建 Qt5

c++ - const boost::array<T,N> 还是 boost::array<const T,N>?

visual-c++ - 如何在托管 C++/CLI 中定义接口(interface)

c++ - 为什么 sleep_for 调用 FreeLibrary?

python - 通过 python distutils 编译带有可重定位设备代码的 cuda 代码(用于 python c 扩展)

c++ - 在 Qt 中捕获拖动取消事件

c++ - boost/asio : Very simple chat server can't access the messages that are recieved

c++ - 变体和 _bstr_t 之间的转换导致 Windows 2008 中的不一致崩溃

c++ - 并排配置不正确