lundi 29 juin 2020

Creating shopping cart

for my class were creating a mini store, and I have the three main functions that work, main.cpp, items.cpp, items.h. but the shoppingcart.cpp and the shoppinfcart.h keeps giving me issues, because I'm trying to pass the parameters from main.cpp, for example (sc1.add(&a) to the functions in shoppingcart.h and shopping cart.cpp.

I have attached copy of my code. any help will be appreciated.

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;
}

a snippet of the code for adding items to vector 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

an example of main main.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;
          }
        }
        

Aucun commentaire:

Enregistrer un commentaire