help with dividing doubles into integers

This is a discussion on help with dividing doubles into integers within the C++ Programming forums, part of the General Programming Boards category; I am trying to compile this program but I keep getting an error message telling me that I'm converting doubles ...

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Question help with dividing doubles into integers

    I am trying to compile this program but I keep getting an error message telling me that I'm converting doubles to integers, which I am trying to do, but I can't get it to compile. It's in the "else if " statement near the bottom. It is trying to divide the double "change" into the integers "twenties, tens, etc."
    This is not important really but the program is supposed to implement a cash register and if the amount tendered is more the amount of the purchase it will show you how much change you owe, according to the number of twenties, tens, fives, etc.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     cout << "Enter an amount equal to the amount of a purchase: $";
     double amount;
     cin >> amount;
     cout << endl << "Enter the amount tendered: $";
     double tender;
     cin >> tender;
     cout << endl;
     double change = tender - amount;
     
     if (tender < amount)
       {
        cout << "The amount tendered was $" << tender << endl;
        cout << "The amount of the purchase was $" << amount << endl;
        cout << "Please tender the additional amount of $" << amount-tender << endl;
       }
      
     if (tender > amount && change == 0)
       {
         cout << "The amount tendered was $" << tender << endl;
         cout << "The amount of the purchase was $" << amount << endl;
         cout << "The total amount of change to be returned to the customer is $" << change << endl;
       }
    
    else if (tender >amount && change > 0)
       {
         cout << "The amount tendered was $" << tender << endl;
         cout << "The amount of the purchase was $" << amount << endl;
         cout << "The amount of change to be returned to the customer is $" << change << endl;
         int twenties = change/20;
         int tens = (change -twenties*20)/10;
         int fives = (change - tens*10)/5;
         int ones = (change - fives*5)/1;
         int quarters = (change - ones*1)/.25;
         int dimes = (change - quarters*.25)/.10;
         int nickels = (change - dimes*.10)/.05;
         int pennies = (changes - nickels*.05)/.01;
         cout << "This will be returned as:" << endl;
         cout << "Twenties: " << twenties << endl;
         cout << "Tens: " << tens << endl;
         cout << "Fives: " << fives << endl;
         cout << "Ones: " << ones << endl;
         cout << "Quarters: " << quarters << endl;
         cout << "Dimes: " << dimes << endl;
         cout << "Nickels: " << nickels << endl;
         cout << "Pennies: " << pennies << endl;
       }
         
            system("pause");
            return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    tender > amount && change == 0
    How it can be?

    Code:
    	int twenties = static_cast<int>(change)/20;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Also, it's preferred to use getchar() instead of system("pause")
    I also see vart react to
    tender > amount && change == 0
    Which brings up the question: if the customers pays more than the exact amount, how can the change be 0? You got a little logic problem there.

    As for the doubles problem... Well, any decimals are floats or doubles. Anything like 0.20 is a double and anything like 0.20f is a float. And when you divide with a double or float, you'll get a float or double as result. And you can't assign a double or float to an int without an explicit cast. So either define all the variables as floats or truncate the result back into an integer via a cast.
    Last edited by Elysia; 02-21-2008 at 04:56 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    wow thanks I completely messed up the logic there huh?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Yes you did... how can the given amount be more than the amount it costs and the change be 0?
    So if I payed more than it cost, does that mean I get no change back?
    Also maybe re-read my edited reply.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. dividing Doubles
    By yukapuka in forum C++ Programming
    Replies: 9
    Last Post: 06-13-2008, 06:39 PM
  4. Please help - dividing doubles
    By hunterdude in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 01:06 AM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

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