C++ Cash Register Help

This is a discussion on C++ Cash Register Help within the C++ Programming forums, part of the General Programming Boards category; I am building a program as a cash register that uses rocks, stones, and pebbles as currency. I seem to ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Unhappy C++ Cash Register Help

    I am building a program as a cash register that uses rocks, stones, and pebbles as currency. I seem to be having problems converting/getting change. This is more of an arithmetic problem rather than a crash or error but if anyone could help out I'd appreciate it.

    Below the code I put some examples.

    Code:
    #include <iostream>#include <string>
    #include <iomanip>
    
    
    const int PEBBLES_PER_ROCK = 25;
    const int PEBBLES_PER_STONE = 5;
    
    
    const int COST_COFFEE = 27;
    const int COST_DONUT = 23;
    
    
    using namespace std;
    
    
    int main () {
    
    
        int coffee = 0,
            donuts = 0,
            rocks = 0,
            stones = 0,
            pebbles = 0;
    
    
        int totalPrice = 0,
            totalPayment = 0,
            totalChange = 0,
            totalRocks = 0,
            totalStones = 0,
            totalPebbles = 0; 
    
    
        int changeRocks = 0,
            changeStones = 0,
            changePebbles = 0;
        
        char junk;
    
    
        cout << "Enter your order => " << endl;
        cin >> coffee >> junk >> donuts >> junk >> rocks >> junk        
            >> stones >> junk >> pebbles;
    
    
        cout << "Cups of Coffee: " << coffee << endl;
             
        cout << "Donuts Purchased: " << donuts << endl;
    
    
        totalPrice = coffee * COST_COFFEE + donuts * COST_DONUT;
        totalRocks = totalPrice / PEBBLES_PER_ROCK;
        totalPrice = totalPrice %= PEBBLES_PER_ROCK;
    
    
        totalStones = totalPrice / PEBBLES_PER_STONE;
        totalPrice = totalPrice %= PEBBLES_PER_STONE;
     
        totalPebbles = totalPrice;
    
    
        cout << "Total Bill: " << totalRocks << " rocks "
             << totalStones << " stones " << totalPebbles << " pebbles " << endl;
    
    
        cout << "Cash tendered: " << rocks << " rocks " << stones << " stones "
             << pebbles << " pebbles " << endl;
        
        totalPayment = rocks * PEBBLES_PER_ROCK + stones * PEBBLES_PER_STONE                         + pebbles;
    
    
        totalPrice = totalRocks * PEBBLES_PER_ROCK + totalStones * PEBBLES_PER_STONE
                   + totalPebbles;
    
    
        totalChange = totalPayment - totalPrice;
    
    
        changeRocks = (totalPayment - totalPrice) / PEBBLES_PER_ROCK;
        totalChange = totalChange %= PEBBLES_PER_ROCK;
    
    
        changeStones = (totalPayment - totalPrice) / PEBBLES_PER_STONE;
        totalChange = totalChange %= PEBBLES_PER_STONE;
    
    
        changePebbles = totalChange;
     
        cout << "Change: " << changeRocks << " rocks " << changeStones << " stones "
             << changePebbles << " pebbles " << endl;
        
    
    
        return 0;
    }
    Now *when running the programming* enter the order in c,d:r|s|p
    c=coffee
    d=donuts
    r=rocks
    s=stones
    p=pebbles

    ex. 3,4:7|0|3
    should result in 0 rocks 1 stones 0 pebbles in change.

    I do get that one right, however I get this one wrong:

    1,1:0|0|80
    should result in 1 rocks 1 stones 0 pebbles in change.
    but I get 1 rocks 6 stones 0 pebbles.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    The problem is on this line:
    Code:
    changeStones = (totalPayment - totalPrice) / PEBBLES_PER_STONE;
    You should have stuck to using totalChange, i.e.,
    Code:
    changeStones = totalChange / PEBBLES_PER_STONE;
    By the way, simplify this:
    Code:
    totalChange = totalChange %= PEBBLES_PER_STONE;
    to:
    Code:
    totalChange %= PEBBLES_PER_STONE;
    This simplification applies to some of your other calculations too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Thanks, I did end up noticing that and made the changes...BUT I'm still having the same problem with change - that just simplified and cleaned my program up.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by nealw
    I'm still having the same problem with change
    A quick test shows that once my suggestions are implemented, the output that you expected is shown.

    By the way, each include must be on its own line, i.e., this:
    Code:
    #include <iostream>#include <string>
    should be:
    Code:
    #include <iostream>
    #include <string>
    Quote Originally Posted by nealw
    that just simplified and cleaned my program up.
    Actually, my compiler reminded me that your overly complex expression has a bigger problem: it results in undefined behaviour. The reason is that you are changing totalChange twice in an expression when you are only allowed to change it once.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    I am sorry, you're right I put the wrong variable in, my apologies.

    It works perfectly, you don't know how glad I am. I even did that change on my own while waiting for a response but you pushing me helped me examine it and try it again. Thanks!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    You're welcome, and it is good that you did not sit around just waiting for an answer
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me fix one problem with my cash register
    By lil_rom in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 12:03 AM
  2. help me fix one problem with my cash register
    By lil_rom in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2008, 02:15 PM
  3. Cash register program help :(
    By lil_rom in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 12:35 AM
  4. A small application of cash-register
    By daisy_polly in forum C Programming
    Replies: 8
    Last Post: 02-02-2006, 06:22 AM
  5. Cash Register
    By Hursh in forum C Programming
    Replies: 5
    Last Post: 01-22-2002, 02:36 AM

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