【问题标题】:Creating shopping cart创建购物车
【发布时间】:2020-10-19 20:38:33
【问题描述】:

我的班级正在创建一个迷你商店,我拥有三个可以工作的主要功能,main.cpp、items.cpp、items.h。但是 shoppingcart.cpp 和 shoppinfcart.h 一直给我带来问题,因为我试图从 main.cpp 传递参数,例如 (sc1.add(&a) 到 shoppingcart.h 和 shoppingcart.cpp 中的函数.

我附上了我的代码副本。任何帮助将不胜感激。

Item.h

#ifndef Items_hpp
#define Items_hpp
#include <string>
#include <iostream>
using namespace std;

class Items {
  private: 
  
  string ItemName;
  float ItemPrice;
  int ItemQuant;
  
  public:
  
  Items();
  
  Items(string in, float ip, int iq);
  
  void Name(string in);
  string entername();
  void CalcPrice( float ip);
  float enterprice();
  void CalcQuant(int iq);
  int enterquant();
  
};
#endif

Items.cpp

#include "Items.h"

Items::Items()
:ItemName("")
,ItemPrice(0)
,ItemQuant(0)
{
}


Items::Items(string in, float ip, int iq)
:ItemName(in)
,ItemPrice(ip)
,ItemQuant(iq)
{
}

void Items:: CalcPrice(float ip)
{
  ItemPrice = ip;
}

void Items::CalcQuant(int iq)
{
  ItemQuant = iq;
}

void Items:: Name(std::string in)
{
  ItemName = in;
}

float Items:: enterprice()
{
  return ItemPrice;
}

int Items:: enterquant()
{
  return ItemQuant;
}

string Items:: entername()
{
  return ItemName;
}

将项目添加到向量的代码的 sn-p ShoppingCart.cpp

#include "ShoppingCart.h" 
#include <vector>

void ShoppingCart ::add(&its)
{
shopitems.push_back(&its);
}

ShoppingCart.h

#ifndef ShoppingCart_hpp
#define ShoppingCart_hpp
#include <iostream>
#include <string>
#include <vector>
#include "Items.h"
using namespace std;


class ShoppingCart
{
  private:
  vector <const char its> shopitems;
  string cname; //customers name
  string cdate; // todays date

  public:
  ShoppingCart();
  ShoppingCart(string cname);
  void add( char *its);
};


#endif

一个主要的例子 ma​​in.cpp

#include <iostream>
#include <string>
#include <vector>
#include "Items.h" 
#include "ShoppingCart.h"
using namespace std;

int main() {
  char choice;
  int itemsbuy;
  float org = .75;
  float appl = .25;
  float pear = 1.00;
  float bana = .85;
  float grape = 6.00;
  float oatmlk = 5.00;
  float almmlk = 6.00;
  float cashmlk= 5.00;
  float soymlk = 4.00;
  float cocomlk = 3.00;
  float twopermlk = 3.00;
  float vitdmlk = 4.00;
  float fatmlk = 4.00;
  float goatmlk = 6.00;
  float rasinbran = 4.00;
  float granola = 5.00;
  float honeybunches = 6.00;
  float twix = 4.00;
  float honey = 5.00;
  float yogurt = 1.50;
  float cashyog = 2.00;
  float eggs = 1.80;
  float vegeggs = 3.00;
  float cheese = 2.00;
  float alcheese = 3.00;
  int itemop;
  int itemno;
  int productcounter = 0;
  int productsum= 0;//might need.
  
  

  cout << "Welcome to J&S Breakfast Grocery store!"<<endl;
  cout << "--------------------------------------"<<endl;
  cout << "How many items will you be purchasing today?"<<endl;
  cout << endl;
  cin >> itemsbuy;
  ShoppingCart sc1;                        //**intializing "shoppingcart.h"**//
  for (int i = 0; i < itemsbuy; i++) {
    cout << "Menu:"<<endl;
    cout << "---------------------------"<<endl;
    cout << "A. Fruits"<<endl;
    cout << "B. Non-Diary"<<endl;
    cout << "C. Diary" <<endl;
    cout << "D. Cereal" <<endl;
    cout << "E. Other"<<endl;
    cout << "Please select your type of items to add to cart"<<endl;
    cin >> choice;
    cout << endl;
    switch (choice) {
      case 'A':
      case 'a':
        cout << "Menu:" <<endl;
        cout << "---------------------------"<<endl;
        cout << "1.Oranges -$ "<< org<<endl;
        cout << "2.Apples -$ "<< appl << endl;
        cout << "3.Pears - $"<<pear<<endl;
        cout << "4.Bananas - $" << bana << endl;
        cout << "5.Grapes - $" << grape << endl;
        cout << endl;
        cout << "Chooose option"<<endl;
        cin >> itemop;
        cout << "How Many?"<<endl;
        cin >> itemno;
        if (itemno > itemsbuy) {
          cout << "Error, cannot add more than youre planning to buy. Please start over."<<endl;
          break;
        }
        else {
        if (itemop ==1) {
          Items a("Oranges" , org, itemno);
          productcounter++;
          sc1.add(&a);                       //**trying to pass parameter here**//
          if (productcounter == itemsbuy) {
           cout<< "Error. You are attempting to buy more than you planned. Please Start over. Thank you."<<endl;
          }
        }
        

【问题讨论】:

  • sc1.add(&amp;a); 变量 a 在最近的 {} 定义的范围内是本地的。当它超出范围时,您存储的指针不再有效。
  • vector &lt;const char its&gt; shopitems; 应该是 vector &lt;Items&gt; shopitems;void add( char *its); 应该是 void add( Items it); 或者 void add( const Items &amp; it);
  • void add( char *its); 应该是 void add(const Item&amp; its) 并且 Shopping.cpp 上的定义应该是:void ShoppingCart::add(const Item&amp; its)
  • 您的所有float 项都应标记为static const,以便编译器可以将常量放入可执行文件的只读部分。
  • 另外,查找std::toupperstd::tolower,这样您就可以将输入全部转换为大写或全部小写,并且只使用if 语句或一个case 语句。跨度>

标签: c++ c++11 parameter-passing


【解决方案1】:

您的代码中有一些错误。

  1. ShoppingCart.h

vector &lt;const char its&gt; shopitems;.

std::vector 在以下语法中声明: std::vector&lt;data type&gt; variable name。 看起来您正在尝试创建一个 Items 的容器并存储它们。因此,您应该使用:而不是vector&lt;const char&gt; shopitemsvector&lt;Items&gt; shopitems 代替。

还有:

void add(char* its); 不正确,因为此方法会将Items 添加到您的shopitems 向量中。将其更改为:void add(const Items&amp; its); 这将传递一个对象Items的const引用,这意味着该方法在调用时不会复制整个类Items,并且该对象将不可修改。

  1. ShoppingCart.cpp

让我们也修复 .cpp 文件中的语法和语义,现在正确的是:

void ShoppingCart::add(const Item& its)
{
    shopitems.push_back(its); //& is not necessary here.
}
  1. Main.cpp

现在,让我们在您的主文件中正确调用该方法。 sc1.add(&amp;a);,这里的: & 告诉它传递对象Items a 所在的内存地址。让我们更正一下,并正常传递对象Items a,只需编写:sc1.add(a);

  1. 逻辑错误

我在您的代码中发现了最后一个逻辑错误,即:if (productcounter == itemsbuy)。如果itemsbuy(您今天要购买的商品数量)为:1。那么productcounter也是1,你什么都买不到,应该是:if (productcounter &gt; itemsbuy)

【讨论】:

  • 谢谢!我会试一试,告诉你发生了什么。感谢您抽出时间来回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 2014-02-27
相关资源
最近更新 更多