Basically i am supposed to create a program where user inputs number of items and create objects out of it and basically i need to store the objects in a file and then read from the file and output the object attributes in console window.Now the problem which i am getting is ,when i create some objects out of it and store the objects in Heap array with separate indexes for separate items so wheneven i am trying to create those objects using the parameterised constructor of Item class i am not getting whether the parameterised constructor is getting called or non-parameterised ,since the output which i am getting is some garbage values and not the values which i am inputting .So please solve my problem.

I tried calling the non-parameterised constructor for creating the heap objects but i don't know why i am getting some garbage values while outputting in my console window.

#include <iostream>
#include <string>
#include <fstream>

class Item {

    private:
        std::string name;
    float price;
    int quantity;

    public:

        std::string getname(void) {

            return this - > name;
        }

    float getprice(void) {
        return this - > price;
    }

    int getquantity(void) {
        return this - > quantity;
    }

    void setname(std::string name) {
        this - > name = name;

    }

    void setprice(float price) {
        if (price > 0.0 f) {
            this - > price = price;
        } else {
            this - > price = 0.1 f;
        }

    }

    void setquantity(int quantity) {
        if (quantity > 0) {
            this - > quantity = quantity;
        } else {
            this - > quantity = 1;
        }
    }

    Item() {

    }

    Item(std::string name, float price, int quantity);

    friend std::ofstream & operator << (std::ofstream & ofs, Item & r);
    friend std::ostream & operator << (std::ostream & out, Item & i);
    friend std::ifstream & operator >> (std::ifstream & ifs, Item & i);

};

Item::Item(std::string name, float price, int quantity) {

    this - > name = name;
    this - > price = price;
    this - > quantity = quantity;
}

std::ofstream & operator << (std::ofstream & ofs, Item & r) {
    ofs << r.name << "\n";
    ofs << r.price << "\n";
    ofs << r.quantity << "\n";

    return ofs;

}

std::ifstream & operator >> (std::ifstream & ifs, Item & i) {

    ifs >> i.name >> i.price >> i.quantity;
    return ifs;
}

std::ostream & operator << (std::ostream & out, Item & i) {

    out << i.name << "\n";
    out << i.price << "\n";
    out << i.quantity << "\n";

    return out;
}

int main(void) {
    int n;

    std::string nm;
    float prc;
    int qty;

    std::cout << "Input the number of items : " << "\n";
    std::cin >> n;

    Item * ptr[n];

    for (int i = 0; i < n; i++) {

        std::cout << "Please input the " << i + 1 << " item's name : " << "\n";
        std::cin >> nm;
        std::cout << "Please input " << i + 1 << " item price :" << "\n";
        std::cin >> prc;
        std::cout << "Please input " << i + 1 << " item quantity : " << "\n";
        std::cin >> qty;
        ptr[i] = new Item(nm, prc, qty);
    }

    /*std::cout<<"\n\n";

    for(int i=0;i<n;i++){


        std::cout<<i+1<<" Items name is : "<<ptr[i]->getname()<<"\n";
        std::cout<<i+1<<" Items price is : "<<ptr[i]->getprice()<<"\n";
        std::cout<<i+1<<" Items quantity is : "<<ptr[i]->getquantity()<<"\n";
        std::cout<<"-----------------------------------\n";


    } */

    std::ofstream ofs("STUDENT.txt");


for (int i = 0; i < n; i++) {
        ofs << * ptr[i];
    }

    Item item;



   std::ifstream ifs("STUDENT.txt");
    for (int i = 0; i < 3; i++) {

        ifs >> item;

        std::cout << "Item : " << i + 1 << "\n" << item << "\n";
    }

    delete[] ptr;

    return 0;
}

0 Answers