vendredi 23 mars 2018

Problems with dynamic memory allocation in class

I am trying to create a class for a pizza restaurant where i have both a separate class for the Pizza and another for the restaurant. The restaurant class has a dynamic array of type Pizza whose size increases everytime another pizza is added to the restaurant.

Whenever i execute the program it stops working in the for loop before a new object is created and the addPizza function is called without giving an error or absolutely anything. I am still new to programming so i think i may be allocating the memory incorrectly thus causing it to freeze. Can anyone please help me on as to what i am doing wrong ?

Thank you.

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

class Pizza {
private:
    char name[15];
    int price;
    char *ingredients;
    int reduction;
public:
    Pizza (char  *name= "", int price= 0, char *ingredients= "", int reduction= 0) {
        this->ingredients= new char [strlen(ingredients)+1];
        strcpy(this->ingredients, ingredients);
        strcpy(this->name, name);
        this->price= price;
        this->reduction= reduction;
    }
    ~Pizza () {
        delete [] ingredients;
    }
    bool areSame(Pizza p) {
        for (int i = 0; i < strlen(this->ingredients) + 1; i++) 
            if (this->ingredients[i] != p.ingredients[i])
                return true;
        return false;            
    }

    int getReduction() {
        return reduction;
    }

    void print() {
        cout << "name: " << name<< " ingredients: " << ingredients<< " Price: " << price;
    }
};

class Pizzeria{
private:
    char name[15];
    Pizza *p;
    int number_of_pizzas;
public:
    Pizzeria(char *name= "") {  
        strcpy(this->name, name);
        p = new Pizza [0];
        number_of_pizzas = 1;
    }

    Pizzeria(const Pizzeria &x) {
        this->p = new Pizza [x.number_of_pizzas-1];
        for (int i = 0; i < number_of_pizzas; i++) {
            this->p[i] = x.p[i];
        }
        this->number_of_pizzas= x.number_of_pizzas;
        strcpy(this->name, x.name);
    }   

    ~Pizzeria() {
        delete [] p;
    }

    void addPizza(Pizza P) {cout<<"fsfas";
        for (int i = 0; i < number_of_pizzas; i++) {
            if (!(p[i].areSame(P))) {
                number_of_pizzas++;
                p[number_of_pizzas] = P;
                return;
            }
        }
    }

    void pizzasOnPromotion() {
        for (int i = 0; i < number_of_pizzas; i++)
            if (p->getReduction() > 0)
                p->print();
    }

    void setName(char *name) {
        strcpy(this->name, name);
    }
    char getName() {
        return *name;
    }
};

int main () {

int n;
char name[15]; 
Pizzeria p1(name);
cin >> name;
cin >> n;
int reduc;

for(int i = 0; i < n; i++){
    char imp[100];
    cin.get();
    cin.getline(imp,100);
    int price;
    cin >> price;
    char ingredients[100];
    cin.get();
    cin.getline(ingredients,100);
    cin >> reduc;   cout << "pls work";
    Pizza p(imp,price,ingredients,reduc);

    p1.addPizza(p);
}

Pizzeria p2 = p1;
cin >> name;
p2.setName(name);
char imp[100];
cin.get();
cin.getline(imp,100);
int price;
cin >> price;
char ingredients[100];
cin.get();
cin.getline(ingredients,100);
cin >> reduc;
Pizza p(imp,price,ingredients,reduc);
p2.addPizza(p);

cout<<p1.getName()<<endl;
cout<<"Pizzas on promotion:"<<endl;
p1.pizzasOnPromotion();

cout<<p2.getName()<<endl;
cout<<"Pizzas on promotion:"<<endl;
p2.pizzasOnPromotion();
return 0;
}

Aucun commentaire:

Enregistrer un commentaire