need help with classes

This is a discussion on need help with classes within the C++ Programming forums, part of the General Programming Boards category; I need some help with my project. I am supposed to input how much money I am to start out ...

  1. #1
    Registered User
    Join Date
    Dec 2003

    Unhappy need help with classes

    I need some help with my project. I am supposed to input how much money I am to start out with in a vending machine and then when I want to purchase a coke or something I insert my money. I want to be able to get the total of these two. i am using the class Money for how much money I am starting out with, and I am not using a class to inster the money. here is my code:
    class Money
    Money(char*pc, int t) {pchange=pc; total=t;}
    int getTotal() {return total;}
    void setTotal(int changeInserted) {total=total+changeInserted;}

    char* pchange;
    int total;

    void main()
    {cout << "Indicate how much money is being inserted: " << endl;
    cout << "Dollar (0 or 1): ";
    cin >> dollars;
    cout << "Quarters: ";
    cin >> quarters;
    cout << "Dimes: ";
    cin >> dimes;
    cout << "Nickels: ";
    cin >> nickels;

    cout << "The total amount of money in the vending machine is: " << endl;
    cout << "Nickels: " << m1.getTotal() << endl;
    cout << "Dimes: " << m2.getTotal() << endl;
    cout << "Quarters: " << m3.getTotal() << endl;
    cout << "Dollars: " << m4.getTotal() << endl;
    void main()

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Mt. Prospect, IL
    first of all PROPER USE OF CODE TAGS!, secondly search this board; very similar vending machine problems have been done many times before......thirdly - void main() ???? always use int main()........

    some entropy with that sink?

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    I'm sorry, but this is just funny. Last time you asked a question, the only response you got was a link to something that shows you how and why to use code tags. Nobody else responded, possibly because many people don't have the time to waste on somebody who does not take the time and effort to make it easier for others to help.

    But the funniest part is the strange code tags at the bottom of both posts (many other new posters have done this also). So let me guess at what happened. You post your code, and then an error pops up saying to please use code tags when posting code with indentation and curly braces. So instead of understanding the point behind the error and the link Salem gave you last time, you just put the code tags at the bottom of the page - "tricking" it into accepting your post. But the funniest thing is that you used the proper code tags, you just didn't put them around your code!! That's just hilarious.

    I apologize for making it seem like I'm laughing at you. It is only the situation that is humorous to me. If you just made a mistake (or two) and didn't understand the point or use of code tags, well here is a pointer. In the window you are typing your post in, first type: [code], then type or paste your code, then type [/code]. Like this:

    int main()
    int num = 3 + 2;

    which ends up looking like this:
    int main()
        int num = 3 + 2;
    Ahhh, much better now.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    As far as your problem goes, I didn't see a question, but it appears you need help understanding how to use classes. Well, your Money class has a constructor that takes a char* and an int. In order to use the class, you must create an instance of the Money class. When creating an instance, you must supply the arguments to pass to one of the available constructors. In this case, there is only one constructor, so you must pass it a char* and an int.

    In your code, you use what appear to be variables called m1, m2, m3, and m4. It looks like they are supposed to be of type Money, but you don't declare them anywhere. (In fact, you don't declare any of the variables in main().) You must declare these variables and initialize them with the appropriate constructor arguments. Here is an example:
    #include <iostream>
    class HelloWorld
        HelloWorld(int num) { savedNum = num; }
        // Ignore this comment until you learn it but I would normally use an initialization list there.
        ~HelloWorld() { }
        void DoYourThing() { std::cout << "Number is: " << savedNum << std::endl; }
        int savedNum;
    int main()
        // Create an instance of HelloWorld named hw1 and pass in 5 to the constructor (your code needs this).
        HelloWorld hw1(5);
        // Use the instance of HelloWorld represented by the variable hw1.
        // Create another instance and use it.
        HelloWorld hw2(7);
        // Not necessary but your compiler might complain (if it does it is "wrong").
        return 0;
    There are still a few other problems with your code, but hopefully this helps answer your "question" for now.

  5. #5
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    1st of all read jlou's second post. As it's relevant to your problem. (the 1st only relates to forum conduct)

    beware the use of C-strings when you use them you almost always have to allocate space to store them because in your constructor the char *pc and the related private member both point to the same memory what if that memory got changed by either pointer? 's always safe to:
    pchange= new char[strlen(pc+1)]; strcpy(pchange, pc);
    that should keep pchange safe for each object you create. And remember since you are allocating memory remember to deallocate in your destructor (every book on C++ will show you how). Everything else has been pointed out by jlou. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21