0

I am having trouble with classes. So I have three different classes: store, customer and product. In the store class I want to be able to add a customer. The problem is the way I need to add the customer does not follow the way customer was called in customer.h. This is what I have from store.h:

void addCustomer(int customerID, string customerName);

in customer.h I have:

Customer();
Customer(string name, int customerID, bool credit);

in customer.cpp I have:

Customer::Customer(string name, int customerID, bool credit) :
name(name), customerID(customerID), credit(credit){}

and in store.cpp I have the following:

void Store::addCustomer(int customerID, string customerName){
    bool credit = false;
    Customer addCustomer(int customerID, string customerName);
    Customer* customer = new Customer(name = "Null", customerID = 0, credit);
    customers.push_back(customer);

    for (int i = 0; i < customers.size(); ++i) {
        if (customers.at(i)->getID() == customerID){
            throw runtime_error("Customer already added.");
        }

    }
}

the exception is to be thrown if the customer has already been added in to the vector. Everything else works for example all of the get and set functions for all three classes but this one block of code wont work and I do not even get an error the program just wont run any further than a certain point. What am I doing wrong?

  • 1
    not sure if thats the problem you are looking for, but it looks a bit strange that you first add a customer to the vector and then if you find it in the vector you throw a runtime error. The only reason this should not create a problem is that it seems you ignore the `customerID` when creating a new customer (but use `0` instead) but then you a trying to find the customer based on that id – 463035818_is_not_an_ai Apr 06 '17 at 13:57
  • also it is not clear at all what this line `Customer addCustomer(int customerID, string customerName);` is supposed to do inside that method – 463035818_is_not_an_ai Apr 06 '17 at 13:57
  • by reading this code I get the impression that you may need to [read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of trial and error – 463035818_is_not_an_ai Apr 06 '17 at 14:00
  • I do not want to add a customer to the list of customers if the customer is already been added. That is the reason for the for loop. – new_programmer_22 Apr 06 '17 at 14:06
  • I tried adding a new customer using the customer definition taken from customer.cpp and replacing the name with customerName given in the addCustomer definition and then replacing the customerID with the customer ID given in the addCustomer definition then I defaulted the credit to false but it kept saying the credit was not defined although it is defined in customer.h and customer.cpp and I included customer.h in store.cpp – new_programmer_22 Apr 06 '17 at 14:10
  • ..but you do push_back a new customer, even before you run the loop. However, the loop will never find and customer with `ID==customerID` because all the customers in the vector have a id of `0`. Your second comment is a bit too confusing/confused (?!?). Why dont you show what you tried together with the error message. Currently there is no way someone could tell you why your programs does not run any further, because there is too little information given (please read about [mcve]) – 463035818_is_not_an_ai Apr 06 '17 at 14:20
  • BTW, you don't need to allocate a `new Customer` in order to insert it into the vector. The `push_back` method will make a copy. Use a local variable to avoid memory leaks. – Thomas Matthews Apr 06 '17 at 15:02

1 Answers1

0

I dont understand the logic of that function. If you want to check if the customer is already in the vector, then you should do that before you add it to the vector, not afterwards. There are lots of stuff in your code that I do not understand and I wonder why you dont get a huge list of compiler errors, however the method could look something like this:

void Store::addCustomer(int customerID, string customerName){
    for (int i = 0; i < customers.size(); ++i) {
        if (customers.at(i).getID() == customerID){
            return; // why runtime error? if the customer is there already 
                    // then just dont add it....
        }
    }
    customers.push_back(Customer(customerName,customerID,false));
}

Note that I assumed that you change the vector from holding pointers to holding objects (ie std::vector<Customer>). Storing pointers in a vector almost never makes sense. Also I would not use a runtime exception here. Runtime exceptions should be for things that are not supposed to happen.

A runtime excpetion would make sense, if you already made sure before that the customer to be added is a new customer. Then a runtime exception would tell you that there is some problem with the code making sure that it is a new customer. However, I suppose that this function is actually the place where you do that check. Thus it is not an exception, but something that can occur during normal program execution. If the calling code needs to know if the customer was added or not, you could make the method return a bool for example.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185