Thread: Damn c++ class, help!!!

  1. #1
    gato_estupido
    Guest

    Angry Damn c++ class, help!!!

    This has been bothering me, the teacher told me to make a cash register type program that you enter the total price of stuff and how much the person paid and it tells you how many 20s 10s 5s 1s quarters dimes nickels and pennies to give as change. I thought oh thats f#cking easy so I started to make it. I see no reason why my code doesnt work and like me and 2 other kids are the only ones who he assigned it to because were the only ones way ahead. They cant figure out why it wont work either. I dont have the source code here right now but it as something like this:

    float change;
    float cash_they_gave;
    float TotalPrice;
    int twentys, tens, fives, ones, quarters, dimes, nickels;


    change = cash_they_gave - TotalPrice;

    while(change>=20)
    {
    change -= 20;
    twentys++;
    }

    while(change>=10)
    {
    change -= 10;
    tens++;
    }

    etc...


    it compiles and but doesnt give me the right numbers.
    please help and not turn my post pink.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    that should be
    Code:
    while(change >= 0) {
           change -= 20;
           twenty++;
    }
    if (change < 0)
          change += 20;
    It might be easier to use integer division along with finding
    the remainder.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Another option would be to use the search feature to find the other 3000 posts asking the exact same question.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    My fault that code won't work.

    Code:
    if (change < 0) {
          change += 20;
          twenty--;
    }
    You should just use division and remainder.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM