Hey guys,

I'm working on my final project for first semester of Freshmen year and I have a favor to ask of you guys. This project is very important for the final grade, so I'd love to know if I've made any errors so far? The program works, but I mean will I see any errors down the road based on my coding?

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

class Vendor
{
    private:
            string name;
            int price;
            int quantity;
            int location;
            char letter;
            int start_quantity;

     public:
            /*Vendor();
            Vendor(string,int,int,int,int);
            ~Vendor();
            void vendItem();*/
            int getQuantity(int);
            string* getName();
            int* getPrice();
            char getAlphaLocation(char);
            int getLocation(int);
            /*int getstartquantity();*/
};

char Vendor::getAlphaLocation(char letter)
{
    switch(letter)
    {
                  case 'A': case 'a':
                                     return letter;
                  case 'B': case 'b':
                                     return letter;
                  case 'C': case 'c':
                                     return letter;
                  case 'D': case 'd':
                                     return letter;
    }
    
}

int Vendor::getLocation(int location)
{
    switch(location)
    {
                  case 1:
                                     return location;
                  case 2:
                                     return location;
                  case 3:
                                     return location;
                  case 4:
                                     return location;
    }
}

int* Vendor::getPrice()
{
    int *price = new int[12];
    price[0] = 25;
    price[1] = 25;
    price[2] = 25;
    price[3] = 25;
    price[4] = 25;
    price[5] = 25;
    price[6] = 25;
    price[7] = 25;
    price[8] = 25;
    price[9] = 25;
    price[10] = 25;
    price[11] = 25;
    
    return price; 
    
}

int Vendor::getQuantity(int quantity)
{
    return quantity;
}

string* Vendor::getName()
{
    //start here
    string *name = new string[12];
    name[0] = "A1. Mars";
    name[1] = "A2. Snickers";
    name[2] = "A3. Twix";
    name[3] = "B1. Kit-Kat";
    name[4] = "B2. Peanut";
    name[5] = "B3. Oreos";
    name[6] = "C1. Reeses's...";
    name[7] = "C2. Hershey's";
    name[8] = "C3. 3 Muske...";
    name[9] = "D1. Ferrero";
    name[10] = "D2. Butterf...";
    name[11] = "D3. Baby Ruth";  
    
    return name;
}

int main()
{
    Vendor user;
    int quantity;
    string* name = user.getName();
    int* price = user.getPrice();
    int location;
    char letter;
    
    for(int i = 0; i < 12; i++)
    {
            cout << name[i] << "\t\t" << price[i] << endl;
    }
    
    cout << "Please enter letter of desired item: ";
    cin >> letter;
    while(letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D' && letter != 'a' && letter != 'b' && letter != 'c' && letter != 'd')
    {
     cout << "Please enter the correct letter: ";
     cin >> letter;
     continue;
    }
    
    cout << "You chose letter " << (char)toupper(user.getAlphaLocation(letter));
    
    cout << "\nPlease enter number of desired item: ";
    cin >> location;
    cout << "You chose number " << user.getLocation(location);
    
    cout << "\nSelect amount: ";
    cin >> quantity;
    cout << "You selected " << user.getQuantity(quantity);
    
    getchar();
    getchar();
    return 0;
}

Dani AI

Generated

Nice start, . A few things will cause trouble later even if it “works” now. You leak memory by allocating arrays in getName/getPrice and never deleting them. Prefer returning values or standard containers by value; the compiler will optimize copies. Several getters shadow parameters and do not read class state (e.g., getQuantity just returns its argument). Also, getAlphaLocation/getLocation have code paths with no return value, which is undefined behavior if the input is invalid. Make getters const and avoid raw pointers.

You can simplify selection with a single index calculation and proper validation. Also, call std::toupper on an unsigned char to avoid UB with negative char values.

#include <array>
#include <cctype>
#include <string>

static int slotIndex(char letter, int number) {
    unsigned char uc = static_cast<unsigned char>(letter);
    char L = static_cast<char>(std::toupper(uc));
    if (L < 'A' || L > 'D' || number < 1 || number > 3) return -1;
    return (L - 'A') * 3 + (number - 1); // 12 slots: A-D x 1-3
}

struct Item { std::string name; int price_cents; int qty; };

class VendingMachine {
    std::array<Item, 12> slots{};
public:
    const Item& slot(int idx) const { return slots.at(idx); }
    bool vend(int idx, int count = 1) {
        auto& s = slots.at(idx);
        if (count <= 0 || s.qty < count) return false;
        s.qty -= count;
        return true;
    }
};

With this layout, the UI code just computes an index, checks for -1, shows slot(idx).name and slot(idx).price_cents, then calls vend. Keep prices in cents to avoid floating-point errors, and format at print time. Finally, separate I/O from logic so you can unit test the class without user input. This will make your final project sturdier and easier to grade and extend.

Hey 2nd poster, I don't know if you're allowed to ask for help in someone else's topic. Also, I doubt anyone help's you if you don't put your code between CODE tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.