-1

I am working on a very vague assignment and really trying my best here.

I almost have it finished, but for some reason my vector is NOT push_back()-ing correctly It is taking a number and pushing it into the vector, but any subsequent pushes, just replace the number in it instead of increasing the size of the vector and adding a new number.

Then, when I try to display the vector it doesn't print anything at all.

I'm still new to this game, so I'm sure there are better methods and easier ways, but I am trying to adhere to this professor's requirements which are as follows:

#01 This program simulates a simple product ordering system. Provide the functionality to allow your user to create new customers, new products, and new orders. Customers will have orders. Orders will have products. For the three classes below, construct class declarations (.h) and class implementations (.cpp) as separate files. Supply appropriate interaction (input/output) with your user. Again, provide appropriate interaction (input/output) with your user.

#02 Class Customer - contains the following: Member data: custID, name, address, orderNums (Class Template vector), customerCreationTime, customerCount - static Member functions: setters and getters for all member data

#03 Class Order - contains the following: Member data: orderNum, custID, productNums (Class Template vector), orderCreationTime, orderCount - static Member functions: setters and getters for all member data

#04 Class Product - contains the following: Member data: productNum, productName, productDescription, productCreationTime, productCount - static Member functions: setters and getters for all member data

#05 Function - void getTime(). When called, provides the date and time via reference parameters. See here for general time example.

#06 Function - void outputReport(). Output a report which lists:
All customers with all member data
All orders with all member data
All products with all member data

These instructions are extremely vague and I decided to try to make a menu program that checks for everything I can think of to pass these requirements.

There is quite a bit of code to look through and I appreciate any help and explanation as to why the code isn't working where the comments (in the TestDriver.cpp code file) specify that it isn't working.

I will update this question with any suggestions.

TestDriver.cpp

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "Customer.h"
    #include "Order.h"
    #include "Product.h"
    #include <algorithm>

    using namespace std;

    void menu(vector<Customer>&, vector<Order>&, vector<Product>&);
    void outputReport(vector<Customer>, vector<Order>&, vector<Product>);
    unsigned int getUnsignedIntegerData(string, int);

    int main()
    {
        char choice{ 'y' };
    vector<Customer> customers;
    vector<Order> orders;
    vector<Product> products;

    // brief program description
    cout << "Welcome to the creation system for Customers, Orders, and Products.\nWould you like to enter a new customer, order or product? (y/n): ";
    cin >> choice;

    // run program
    while (choice == 'y')
    {
        // right now, menu() is the entire program with exception to the getUnsignedInteger method
        menu(customers, orders, products);
        choice = 'n';
    }

    cout << "\nPrinting Report..." << endl;
    outputReport(customers, orders, products);

    return 0;
} // end of main()

void menu(vector<Customer>& customers, vector<Order>& orders, vector<Product>& products)
{
    unsigned int custID{ 0 };
    unsigned int orderNum{ 0 };
    unsigned int prodNum{ 0 };

    string name{ "" };
    string address{ "" };
    string prodName{ "" };
    string prodDesc{ "" };

    int menuChoice{ 1 };

    char choice{ 'n' };

    bool custFound{ false };
    bool orderFound{ false };
    bool prodFound{ false };

    // this is where the program begins
    do
    {
        // displays and collects an integer input for menu selection
        // there is a flag, which just checks the numbers are between 1-4
        menuChoice = getUnsignedIntegerData("     1) Create Customer\n     2) Create Order\n     3) Create Product\n     4) Quit\nSelection: ", 0);

        // 1) Create Customer
        if (menuChoice == 1)
        {
            custFound = false;
            
            // create customer
            cout << "\nCreate customer...\n" << endl;

            // collect customer ID number
            custID = getUnsignedIntegerData("ID for customer: ", 1);
            cin.ignore();

            // loop through customers vector
            for (Customer cust : customers)
            {
                // if customer ID is found in vector, dont allow creation of new customer with that ID
                if (custID == cust.getCustID())
                {
                    cout << "There is a customer with that ID already in the database. Please choose another ID" << endl;
                    custFound = true;
                } // end of if()
            } // end of for()

            // if ID isn't present in customers vector..
            if (!custFound)
            {
                // collect name of customer
                cout << "\nName of customer: ";
                getline(cin, name);

                // collect address of customer
                cout << "\nAddress of customer: ";
                getline(cin, address);

                // create customer and add to list of customers
                Customer customer(custID, name, address);
                customers.push_back(customer);
            } // end of if()
        } // end of if()
        // 2) Create order
        else if (menuChoice == 2)
        {
            custFound = false;
            prodFound = false;

            // create order
            cout << "\nCreate Order...\n" << endl;

            // check if customers is empty
            if (Customer::getCustomerCount() == 0)
            {
                cout << "No customers in database. Create a customer first, then try again." << endl;
            } // end of if()
            // check if products is empty
            else if (Product::getProductCount() == 0)
            {
                cout << "No products to make an order with. Create a product first, then try again." << endl;
            }
            else
            {
                // collect customer ID to create an order for that customer
                // the flag validates the input is non negative and not zero
                custID = getUnsignedIntegerData("What customer is this order for? Enter customer ID: ", 1);

                // look for customer in database
                for (Customer cust : customers)
                {
                    // if the customer ID exists, allow creation for new order for that customer
                    if (custID == cust.getCustID())
                    {
                        custFound = true;
                        cout << "Enter order information for cust #" << custID << ": " << cust.getName() << endl;
                    } // end of if()
                } // end of for()

                // if customer was found by ID...
                if (custFound)
                {
                    // collect order number
                    // flag 2 is for validating the order number is non negative, not zero, and displays proper message
                    orderNum = getUnsignedIntegerData("Enter order number: ", 2);

                    // loop through orders
                    for (Order order : orders)
                    {
                        // if the order number is found in the database, don't allow creation of new order with that order number
                        if (orderNum == order.getOrderNum())
                        {
                            orderFound = true;
                            cout << "Order with order number " << orderNum << " already exists. Please try a different order number" << endl;
                        } // end of if()
                    } // end of for()
                        
                    // if the order number is a new number...
                    if (!orderFound)
                    {
                        choice = 'y';

                        do
                        {
                            // collect product number
                            // flag 3 is for validating the number is non negative, non zero, and displays a proper message
                            cout << "Enter product number for order #" << orderNum << ": ";
                            prodNum = getUnsignedIntegerData("", 3);

                            // loop through products vector
                            for (Product product : products)
                            {
                                // if product number is present in vector, display it's number and name
                                if (prodNum == product.getProductNum())
                                {
                                    prodFound = true;

                                    cout << "Adding product no. " << product.getProductNum() << " - \"" << product.getProductName() << "\" to order." << endl;
                                } // end of if()
                            } // end of for()

                            // if product number was found..
                            if (prodFound)
                            {
                                // loop through customers
                                for (Customer cust : customers)
                                {
                                    // if customer is found by ID
                                    if (custID == cust.getCustID())
                                    {
                                        // second and then on runs of adding a product
                                        if (orderFound)
                                        {
                                            // search for order by customer ID and add the prod num to the order object
                                            for (Order order : orders)
                                            {
                                                if (cust.getCustID() == order.getCustID())
                                                {
                                                    order.setProductNums(prodNum);
                                                } // end of if()
                                            } // end of for()
                                        } // end of if()
                                        else
                                        {
                                            orderFound = true;

                                            // create new order with order number and customer id to link by ID to each customer
                                            orders.push_back(Order(orderNum, custID));

                                            // loop through orders vector
                                            for (Order order : orders)
                                            {
                                                // if customer ID and order customer ID match, then a customer is found
                                                if (cust.getCustID() == order.getCustID())
                                                {
                                                    // THIS IS PUSHING ONLY 1 NUMBER, AND REPLACING THE NUMBER EACH TIME. 
                                                    // IT SHOULD BE PUSH_BACKing AN UNSIGNED INT TO A VECTOR IN ORDER
                                                    // IT PUSHES AN INT, AND THEN REPLACES IT EVERY SUBSEQUENT ITERATION
                                                    order.setProductNums(prodNum);
                                                } // end of if()
                                            } // end of for()
                                        } // end of else()

                                        // add order number to customer order numbers vector (this is working. so i really don't know why
                                        // the above code isn't working for the product numbers)
                                        cust.setOrderNums(orderNum);
                                    } // end of if()
                                } // end of for()
                            } // end of if()
                            else
                            {
                                cout << "No product matching that product number was found." << endl;
                            }

                            cout << "Enter another product? (y/n): ";
                            cin >> choice;
                        } while (choice == 'y');
                    }
                }
                else
                {
                    cout << "Please check the ID you entered. Customer with id " << custID << " has not been found." << endl;
                }
            }

            orderFound = false;
        }
        // 3) Create Product
        else if (menuChoice == 3)
        {
            prodFound = false;

            // create product
            cout << "\nCreate product...\n" << endl;

            // collect product ID
            // flag 3 validates product ID is non negative, not zero, and displays proper message
            prodNum = getUnsignedIntegerData("Enter product number: ", 3);
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            // verify the product doesn't exist by product number
            for (Product product : products)
            {
                // if product number is in products vector, don't allow creation with that product number
                if (prodNum == product.getProductNum())
                {
                    prodFound = true;
                } // end of if()
            } // end of for()

            // if the product number exists, display message
            if (prodFound)
            {
                prodFound = true;
                cout << "Product with this product number already exists. Please try another number" << endl;
            }
            else
            {
                // product ID doesn't exist, collect prod name
                cout << "Enter product name: ";
                getline(cin, prodName);

                // ask for description
                cout << "What is this product's description: ";
                getline(cin, prodDesc);

                // push new product with product number, product name, and product description to products vector
                products.push_back(Product(prodNum, prodName, prodDesc));
            } // end else()
        } // end else if()
        
        cout << "\nReturning to main menu..\n" << endl;
    } while (menuChoice != 4);
} // end of menu()

// this function should display each customer, their orders, and the products in each order
// it is displaying the customers, their orders, but NOT the products in each order
// this is the 2nd broken thing in my program
void outputReport(vector<Customer> customers, vector<Order>& orders, vector<Product> products)
{
    // display total number of customers
    cout << "Customers in database: " << Customer::getCustomerCount() << endl;

    // if there are customers...
    if (Customer::getCustomerCount() > 0)
    {
        // and there are orders...
        if (Order::getOrderCount() > 0)
        {
            // display total number of orders
            cout << "Total orders in database: " << Order::getOrderCount() << endl;

            // if there are products (previous check makes sure there are)
            if (Product::getProductCount() > 0)
            {
                // display total number of products
                cout << "Total products in database: " << Product::getProductCount() << endl;

                cout << "Printing database..." << endl << endl;
                
                //print each customer, their info, the order numbers associated with each customer and the order info, the products associated with each order and their info       
                // loop through each customer
                for (Customer cust : customers)
                {
                    // display customer info
                    cout << "Cust ID:    " << cust.getCustID() << endl;
                    cout << "Name:       " << cust.getName() << endl;
                    cout << "Address:    " << cust.getAddress() << endl;
                    cout << "Created At: " << cust.getCustomerCreationTime() << endl;
                    cout << "\nOrders in profile:" << endl;

                    // loop through orders
                    for (Order order : orders)
                    {
                        // find customer ID and match to order customer ID
                        if (cust.getCustID() == order.getCustID())
                        {
                            // display order info
                            cout << "       Order Number: " << order.getOrderNum() << endl;
                            cout << "       Created At:   " << order.getOrderCreationTime() << endl;
                            cout << "\n       Product(s) in order:" << endl;



                            // this isnt printing at all
                            // i set a message in getProductNums() to loop through the data it is returning, but it is NOT doing anything at all.
                            // it is not displaying a message as though the method is never being called.

                            // loop through each product number in the customer's current order
                            for (unsigned int prodNum : order.getProductNums())
                            {
                                // loop through products vector
                                for (Product product : products)
                                {
                                    // if product number from order.getProductNums() matches a product number, display that product's info
                                    if (prodNum == product.getProductNum())
                                    {
                                        // display product info
                                        cout << "Product Number: " << product.getProductNum() << endl;
                                        cout << "Product Name:   " << product.getProductName() << endl;
                                    } // end of if()
                                } // end of for()
                            } // end of for()
                        } // end of if()

                        cout << endl;
                    } // end of for()

                    cout << endl;
                } // end of for()
            } // end of if()
            else
            {
                cout << "No products. Nothing to report." << endl;
            } // end of else()
        } // end of if()
        else
        {
            cout << "No orders. Nothing to report." << endl;
        } // end of else()
    } // end of if()
    else
    {
        cout << "No customers. Nothing to report." << endl;
    } // end of else()
} // end of outputReport()

// not typing comments for this method right now
unsigned int getUnsignedIntegerData(string prompt, int flag)
{
    int value{ 0 };

    while (true)
    {
        cout << prompt;

        while (!(cin >> value))
        {
            if (flag == 0)
            {
                cout << "Please enter a proper selection." << endl;
            }
            else if (flag == 1)
            {
                cout << "Please enter a proper customer ID (ex: 12345)" << endl;
            }
            else if (flag == 2)
            {
                cout << "Please enter a proper order number (ex: 12345)" << endl;
            }
            
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << prompt;
        }

        if (value <= 0 && flag == 1)
        {
            cout << "Customer ID cannot be less than or equal to zero" << endl;
        }
        else if ((value < 1 || value > 4) && flag == 0)
        {
            cout << "Please make a valid selection (1-4)" << endl;
        }
        else if (value <= 0 && flag == 2)
        {
            cout << "Order number cannot be less than or equal to zero" << endl;
        }
        else if (value <= 0 && flag == 3)
        {
            cout << "Product number cannot be less than or equal to zero" <<  endl;
        }
        else
        {
            return value;
        }
    }
}

Customer.h and Customer.cpp

    #include <vector>
    #include "Order.h"
    
    #ifndef CUSTOMER_H
    #define CUSTOMER_H

    class Customer
    {
        public:
        Customer(unsigned int, std::string, std::string);
        void setCustID(unsigned int);
        void setName(std::string);
        void setAddress(std::string);
        void setOrderNums(unsigned int);
        void setCustomerCreationTime(std::string);
        unsigned int getCustID();
        std::string getName();
        std::string getAddress();
        std::vector<unsigned int> getOrderNums() const;
        std::string getCustomerCreationTime();
        static unsigned int getCustomerCount();
    private:
        unsigned int custID{ 0 };
        std::string name{ "" };
        std::string address{ "" };
        std::vector<unsigned int> orderNums;
        std::string customerCreationTime;
        static unsigned int customerCount;
    };

    #endif
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <vector>
    #include "Customer.h"
    #include "Time.h"

    unsigned int Customer::customerCount{ 0 };

    Customer::Customer(unsigned int custID, std::string name, std::string address)
    {
    getTime(customerCreationTime);
    setCustID(custID);
    setName(name);
    setAddress(address);
    customerCount++;
    }

/*
*
* SETTERS
*
*/
void Customer::setCustID(unsigned int custID)
{
    this->custID = custID;
}

void Customer::setName(std::string name)
{
    this->name = name;
}

void Customer::setAddress(std::string address)
{
    this->address = address;
}

void Customer::setOrderNums(unsigned int orderNum)
{
    this->orderNums.push_back(orderNum);
}

void Customer::setCustomerCreationTime(std::string custCreationTime)
{
    this->customerCreationTime = custCreationTime;
}

/*
*
* GETTERS
*
*/
unsigned int Customer::getCustID()
{
    return custID;
}

std::string Customer::getName()
{
    return name;
}

std::string Customer::getAddress()
{
    return address;
}

std::vector<unsigned int> Customer::getOrderNums() const
{
    return orderNums;
}

std::string Customer::getCustomerCreationTime()
{
    return customerCreationTime;
}

unsigned int Customer::getCustomerCount()
{
    return customerCount;
}

Order.h and Order.cpp

#include <vector>

#ifndef ORDER_H
#define ORDER_H

class Order
{
    public:
        Order(unsigned int, unsigned int);
        void setOrderNum(unsigned int);
        void setCustID(unsigned int);
        void setProductNums(unsigned int);
        void setOrderCreationTime(std::string);
        unsigned int getOrderNum();
        unsigned int getCustID();
        std::vector<unsigned int> getProductNums() const;
        std::string getOrderCreationTime();
        static unsigned int getOrderCount();
    private:
        unsigned int orderNum{ 0 };
        unsigned int custID{ 0 };
        std::vector<unsigned int> productNums;
        std::string orderCreationTime;
        static unsigned int orderCount;
};

#endif
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include "Order.h"
#include "Time.h"

unsigned int Order::orderCount{ 0 };

Order::Order(unsigned int orderNum, unsigned int custID)
{
    getTime(orderCreationTime);
    setCustID(custID);
    setOrderNum(orderNum);
    orderCount++;
}

/*
*
* SETTERS
*
*/
void Order::setOrderNum(unsigned int orderNum)
{
    this->orderNum = orderNum;
}

void Order::setCustID(unsigned int custID)
{
    this->custID = custID;
}

void Order::setOrderCreationTime(std::string orderCreationTime)
{
    this->orderCreationTime = orderCreationTime;
}

void Order::setProductNums(unsigned int productNum)
{
    this->productNums.push_back(productNum);

    for (unsigned int product : productNums)
    {
        std::cout << "PRODUCT NUMS: " << product << std::endl;
    }
}

/*
*
* GETTERS
*
*/
unsigned int Order::getOrderNum()
{
    return orderNum;
}

unsigned int Order::getCustID()
{
    return custID;
}

std::string Order::getOrderCreationTime()
{
    return orderCreationTime;
}

std::vector<unsigned int> Order::getProductNums() const
{
    for (unsigned int item : productNums)
    {
        std::cout << "PRODUCT.CPP RETURNING - " << item << std::endl;
    }

    return productNums;
}

unsigned int Order::getOrderCount()
{
    return orderCount;
}

I have tried adding lines of code in the class file's methods to see what information it was 'setting' and 'getting', but those were working as expected except for Order's setProductNums(), which is the exact same code - basically - as Customer's setOrderNums(). So I have no idea why it isn't working the way it should. Customer is working just fine, so it's somewhere else in my code.

I have tried other things, but I am honestly so tired from trying to figure this out that my brain is just not working right now.

If I remember other things I have tried / If I try other things, I will update the question.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
johnthor
  • 1
  • 5
  • " have tried adding lines of code in the class file's methods to see what information it was 'setting' and 'getting', " Are you using a debugger? They are great for this sort of thing. – doug Aug 26 '23 at 02:50
  • 5
    Get some sleep, clear your mind, and read up on how to provide a [mcve]. You have provided a heap of code and a lot of text, but haven't done anything to help narrow down the problem. You're basically forcing anyone who might try to help you to wade through a lot of irrelevant stuff in the hope of finding the problem. – Peter Aug 26 '23 at 02:50
  • My *wild guess*, since every usage of `order.setProductNums(prodNum)` is wrapped in a `if (cust.getCustID() == order.getCustID())`, is that you are expecting `cust.getCustID()` to be equal to `order.getCustID()` but it actually isn't. – Peter Aug 26 '23 at 02:57
  • StackOverflow is a Q&A site, not a debugging service. This is WAY too much code to ask anyone to look through. Please reduce the code to a [mcve] that demonstrates the problem in action. That being said, the symptoms you describe sound like *undefined behavior* to me, like if you are calling methods on invalid objects, or something like that. Please learn how to debug your code, it is a vital skill every programmer needs to know. . – Remy Lebeau Aug 26 '23 at 04:21

0 Answers0