Everything summing to Zero problem

This is a discussion on Everything summing to Zero problem within the C++ Programming forums, part of the General Programming Boards category; Hi guys/gals, Not sure if this forum answers novice questions or not, but I'm having a problem figuring out why ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Everything summing to Zero problem

    Hi guys/gals,

    Not sure if this forum answers novice questions or not, but I'm having a problem figuring out why this program sums everything to Zero. In the total_euros, which is the final "amount" displayed to the user after they input their values, it always shows Zero euros, instead of the converted amount. I know this is simple, but I cannot seem to figure out what I am doing wrong. Any suggestions?

    Code:
    #include <iostream>
    using namespace std;
    
    #define SICKLES_TO_GALLEONS 17
    #define KNUTS_TO_SICKLES 29
    #define EUROS_TO_GALLEONS 60
    
    #define COMMISSION 0.05
    
    #define KNUTS_TO_GALLEONS (KNUTS_TO_SICKLES * SICKLES_TO_GALLEONS)
    #define EUROS_TO_KNUTS (EUROS_TO_GALLEONS / KNUTS_TO_GALLEONS)
    
    int main()
    
    {
    
    int galleons;
    int sickles;
    int knuts;
    
    int total_sickles;
    int total_knuts;
    
    
    // We first convert everything to Knuts
    //To turn from knut to euros, 
    
    cout << "Enter amount of galleons\n";
    cin >> galleons;
    cout << "Enter amount of sickles\n";
    cin >> sickles;
    cout << "Enter amount of knuts\n";
    cin >> knuts;
    
    total_sickles = (galleons * SICKLES_TO_GALLEONS) + sickles;
    
    cout << "Right now you have" <<total_sickles << "sickles\n";
    
    total_knuts = (total_sickles * KNUTS_TO_SICKLES) + knuts;
    cout << "Right now you have" <<total_knuts << "Knuts";
    
    // Then convert the nuts to euros (60--euros in 1 galleon/493knuts in one galleon),
    // so that's total nuts * (60/493)
    
    double total_euros = (total_knuts * EUROS_TO_KNUTS);
    
    cout << "For the amount you entered, you get" << total_euros <<"Euros";
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    EUROS_TO_KNUTS is equal to (60/KNUTS_TO_GALLEONS), where KNUTS_TO_GALLEONS = (29*17), so it equals (60/(29*17)). Since this is evaluated using integer arithmetic, and it's less than 1, it's truncated to zero. Try putting a decimal point after each of the integers to make them double, so all of the arithmetic is done using double arithmetic.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Wow, that actually, worked. Thanks, man. I guess I need to read up on using #define again. I thought that when you use #define, it was automatically forced to convert the values as you changed your types through out your code, liek from int to double or whatever. Anyway, thanks again.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It doesn't do any conversion, it's like copy/pasting the value in.

    double total_euros = (total_knuts * (60/(29*17)));

    would have the same issue. It converts only after the entire right hand side is done evaluating.
    Last edited by Cat; 11-24-2006 at 11:35 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It would be better in this case to use consts, as in
    Code:
    const double sickles_to_galleons = 17;
    etc. This way the compiler knows these are double quantities and automatically does the arithmetic the right way. You can also put them inside main() so they're local to main(). You also don't need parentheses in expressions like
    Code:
    euros_to_knuts = euros_to_galleons/knuts_to_galleons;
    anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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