0

Ill put my code at the end, as not to scare off anyone from reading on past the code (as if something so simple could scare!)

I basically wrote this tonight. I have been working on my C++ skills, specifically in the way of learning newer concepts...such as implementing OOP, pointers, ect. Stuff i have never used before a couple years back during my Java class.

I am pretty happy with the way it turned out. I ran into very few major errors that gave me any trouble other than a "*" that looked a lot like a period "." . :lol:

I basically want any suggestions on what to do, how to improve anything. Also wanted to know if this would have been more simple to write using some other data type, such as a struct or something for example.

// Store creation for practicing Classes in C++
// Woobag

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

float TOTAL_PRICE = 0.0;

class cFood {
    string type;
    float price;

public:
    void setvalues(string, float);
    string gettype(void);
    float getprice(void);
};

void cFood::setvalues(string i, float f) {
    type = i;
    price = f;
}

string cFood::gettype(void) { return type; }

float cFood::getprice(void) { return price; }

// end of class declarations and implementations

float cashReg(float money) {
    money -= TOTAL_PRICE;
    return money;
}

int main() {
    cFood meat, vegetables, dessert;

    meat.setvalues("Meat", 1.50);
    vegetables.setvalues("Vegetables", .75);
    dessert.setvalues("Desserts", 3.50);
    
    float numA=0, numB=0, numC=0, cash=0;

    cout << "Enter the number of pounds of " << meat.gettype() 
         << " you would like to purchase ($" << meat.getprice() << "):";
    cin >> numA;

    cout << "\nEnter the number of pounds of " << vegetables.gettype() 
         << " you would like to purchase ($" << vegetables.getprice() << "):";
    cin >> numB;

    cout << "\nEnter the number of " << dessert.gettype() 
         << " you would like to purchase ($" << dessert.getprice() << "):";
    cin >> numC;

    TOTAL_PRICE = (meat.getprice() * numA) + (vegetables.getprice() * numB) + (dessert.getprice() * numC);

    cout << "\nYour total cost is: $" << TOTAL_PRICE << ". Please enter your cash: ";
    cin >> cash;

    cout << "\nYour change is: $" << cashReg(cash) << ". Have a nice day!";

    cin.ignore(2);
}

Its my first post by the way! im looking forward to learning and growing with this community! looks like a fabulous place of knowledge.

Cheers!
- Woobag

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

just a few coding style changes -- I like to make one-line functions inline -- saves keyboard typing :cheesy:

// Store creation for practicing Classes in C++
// Woobag

float TOTAL_PRICE = 0.0;

class cFood {
protected:
    string type;
    float price;

public:
    void setvalues(string, float) {type = i; price = f;}
    string gettype(void) {return type;}
    float getprice(void) {return price;}
};


// end of class declarations and implementations

inline float cashReg(float money) {
    return money - TOTAL_PRICE;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.