0
 INPUT STDIN -> <street> <city> <house_number> <number of objects of house> <object1> <price1> .......<object-n> <price-n> (until EOF)

I need to use the "add" method in the "House" Class.
objective: adding the specific n objects of each House in "House" class

This is what i did since now:

#include <iostream>
#include <utility>
#include<vector>
#include<string>
using namespace std;

class Object {
public:
    string valuable;
    float price;
public:
    Object() : Object("",0) {}
    Object(string v, float p) : valuable(std::move(v)), price(p) {}

    string getValuable() {
        return valuable;
    }
    float getPrice()  {
        return price;
    }
};

class House{
public:
    string street;
    string city;
    uint32_t number;
    vector<Object>valuables;
public:
    House(): House("","",0){}
    House(string s,string c,uint32_t n): street(std::move(s)),city(std::move(c)),number(n){}
    string getStreet() {
        return street;
    }
    string getCity() {
        return city;
    }
    uint32_t getNumber() {
        return number;
    }
    uint32_t getValuablesSize() {
        return valuables.size();
    }
    Object getValuable(uint32_t x){
        return valuables[x];
    }
    void add(Object a){
        valuables.emplace_back(a);
    }

};

float getTotalPrice(House a) {
    float sum = 0;
    for (int i = 0; i < a.getValuablesSize(); i++) {
        sum +=a.valuables[i].getPrice();
    }
    return sum;
}

int main() {

    vector<Object>obj;
    vector<House>house;
    char object[30],street[30],city[30];
    float price;
    uint32_t house_number;
    int n;
    while(cin>>street>>city>>house_number>>n) {
        house.emplace_back(string(street),string(city),house_number);
        Object a;
        for(int i=0;i<n;i++){
            cin>>object>>price;  
            obj.emplace_back(object,price);
            a.valuable=object;
            a.price=price;
            for(int k=0;k<house.size();k++)
            house[k].add(a);          
        }      
    }
    for(int i=0;i<obj.size();i++){
        cout<<obj[i].getValuable()<<" "<<obj[i].getPrice()<<endl;
   } // trying to print the object vector
    for(int i=0;i<house.size();i++){ //trying to verify if i have the correct input
        cout<<house[i].getStreet()<<" "<<house[i].getCity()<<" "<<house[i].getNumber()<<" ";
        for(int j=0;j<house[i].getValuablesSize();j++) {
            cout << house[i].valuables[j].valuable<< " "<<house[i].valuables[j].price<<" ";
        } 
        cout<<endl;
    }

    return 0;
}

That's what i think: -when i read <house_number> ,read the objects and prices and then the add method should be used in order to have the vector<Object>valuables usable.
It's necesarly to check if the input is stored corectly in the class "House", in order to continue summing the objects in every house

1
  • Side note, you should prefer std::string to char[] in most cases Commented Oct 25, 2021 at 11:59

1 Answer 1

1

With the statements

for(int k=0;k<house.size();k++)
    house[k].add(a);

you add the current "valuable" object to every house that has been created thus far.

I suggest you instead create the house object separately, then add the valuable objects to the current house, and after that add the house to your collection of houses.

Perhaps something like:

std::string street;
std::string city;
unsigned house_number;
unsigned n;

while(std::cin >> street >> city >> house_number >> n) {
    House current_house(street, city, house_number);

    std::string object;
    float price;

    for(int i = 0; i < n && std::cin >> object >> price; ++i) {
        Object a(object, price)
        current_house.add(a);
    }

    house.push_back(current_house);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, it worked! i didnt think like that, but good to know from now, cause 1 week ago i just started learing OOP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.