C++ 创建购物车
对于我的班级,我们创建了一个迷你商店,我有三个主要的功能,main.cpp,items.cpp,items.h。但是shoppingcart.cpp和shoppinfcart.h不断给我带来问题,因为我试图将参数从main.cpp传递给shoppingcart.h和shoppingcart.cpp中的函数,例如(sc1.add(&a) 我已附上我的代码副本。任何帮助将不胜感激 项目.hC++ 创建购物车,c++,c++11,parameter-passing,C++,C++11,Parameter Passing,对于我的班级,我们创建了一个迷你商店,我有三个主要的功能,main.cpp,items.cpp,items.h。但是shoppingcart.cpp和shoppinfcart.h不断给我带来问题,因为我试图将参数从main.cpp传递给shoppingcart.h和shoppingcart.cpp中的函数,例如(sc1.add(&a) 我已附上我的代码副本。任何帮助将不胜感激 项目.h #ifndef Items_hpp #define Items_hpp #include <string
#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
#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
用于向向量添加项的代码片段
ShoppingCart.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;
}
#include "ShoppingCart.h"
#include <vector>
void ShoppingCart ::add(&its)
{
shopitems.push_back(&its);
}
#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;
}
}
#包括“ShoppingCart.h”
#包括
作废ShoppingCart::添加(&its)
{
shopitems.push_-back(&its);
}
ShoppingCart.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
#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
\ifndef ShoppingCart\u水电站
#定义ShoppingCart\u水电站
#包括
#包括
#包括
#包括“项目.h”
使用名称空间std;
类购物车
{
私人:
矢量商店物品;
字符串cname;//客户名称
字符串cdate;//今天的日期
公众:
购物车();
购物车(字符串cname);
无效添加(字符*its);
};
#恩迪夫
一个主要的例子
main.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;
}
#include "ShoppingCart.h"
#include <vector>
void ShoppingCart ::add(&its)
{
shopitems.push_back(&its);
}
#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;
}
}
#包括
#包括
#包括
#包括“项目.h”
#包括“ShoppingCart.h”
使用名称空间std;
int main(){
字符选择;
int itemsbuy;
浮动组织=0.75;
浮动应用程序=.25;
浮梨=1.00;
浮动bana=.85;
浮葡萄=6.00;
浮点数=5.00;
浮动almmlk=6.00;
浮动现金MLK=5.00;
浮动大豆Mlk=4.00;
浮动cocomlk=3.00;
浮点数twopermlk=3.00;
浮动vitdmlk=4.00;
浮动fatmlk=4.00;
浮动goatmlk=6.00;
浮动rasinbran=4.00;
浮动格兰诺拉麦片=5.00;
浮子蜜束=6.00;
浮动twix=4.00;
漂浮蜂蜜=5.00;
漂浮酸奶=1.50;
浮动现金yog=2.00;
浮卵=1.80;
浮鸡蛋=3.00;
浮奶酪=2.00;
浮点数=3.00;
int itemop;
国际项目编号;
int productcounter=0;
int productsum=0;//可能需要。
你的代码中有一些错误
ShoppingCart.h:
vector shopitems;
std::vector
以以下语法声明:
std::vector变量名
。
看起来您正在尝试创建项目的容器并存储它们。因此,您应该使用以下内容,而不是vector shopitems
:
vector shopitems
此外:
void add(char*its);
不正确,因为此方法将向shopitems
向量添加Items
。将其更改为:void add(const Items&its);
这将传递对象项
的常量引用,这意味着调用该方法时不会复制整个类项
,并且该对象将不可修改
ShoppingCart.cpp:
我们还要修复.cpp文件中的语法和语义,现在正确的方法是:
void ShoppingCart::add(const Item& its)
{
shopitems.push_back(its); //& is not necessary here.
}
Main.cpp
现在,在您的主文件中,让我们正确地调用该方法。sc1.add(&a)
,此处的:&告诉它传递对象项a
所在位置的内存地址。让我们更正此问题,并正常地传递对象项a
,只需编写:sc1.add(a);
逻辑错误
我在你的代码中发现了最后一个逻辑错误,那就是:if(productcounter==itemsbuy)
。如果itemsbuy
(你今天想买的物品数量)是:1。那么,productcounter
也是1,你不能买任何东西,这应该是:if(productcounter>itemsbuy)
sc1.add(&a);
变量a
是最近的{}定义的范围的局部变量。当它超出范围时,您存储的指针不再有效。向量shopitems;
应该是向量shopitems;
和无效添加(char*its);
应该是无效添加(char*its)
或者可能无效添加(const Items&it);
和无效添加(char*its);
应该是无效添加(const Item&its)
和Shopping.cpp的定义应该是:无效购物车::添加(const Item&its)
所有的float
项都应该标记为static const
,这样编译器就可以将常量放入可执行文件的只读部分。此外,还可以查找std::toupper
和std::tolower
,这样就可以将输入转换为所有大写或所有小写,并且只使用if
s陈述或一个案例
陈述。谢谢!我会尝试一下,告诉你发生了什么。谢谢你抽出时间回答!