Need some help

This is a discussion on Need some help within the C++ Programming forums, part of the General Programming Boards category; I am learning C++, working through the books recommended by this website. I'm trying to code Exercise 6-3 of Practical ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    11

    Need some help

    I am learning C++, working through the books recommended by this website. I'm trying to code Exercise 6-3 of Practical C++ Programming. Here's the problem:

    Given an amount less than $1.00, compute the number of quarters, dimes, nickels, and pennies needed.

    Here's my code:

    Code:
    #include <iostream>
    
    int main(){
    	float money; // amount of money
    	int quarters, dimes, nickels, pennies;  // Stores the amount of each coin needed.
    
    	while (true){
    		std::cout<< "Enter a non-negative amount of money " // Prompt user for money
    			<< "less than $1.00, or -1 to stop: ";	    // amount
    		std::cin >> money;
    
    		if (money == -1) // Loop until user enters -1
    			break;
    
    		if (money < -1 || money >= 1.0) // Start loop over if money
    			continue;		// amount is invalid.
    
    		std::cout << "$" << money << " in change is ";
    
    		quarters = (money / 0.25);  // Stores the amount of quarters needed, then subtracts
    		money -= (quarters * 0.25); // that amount in cents from the money total.
    		
    		dimes = (money / 0.1);  // Stores the amount of dimes needed, then subtracts
    		money -= (dimes * 0.1); // that amount in cents from the money total.
    
    		nickels = (money / 0.05);  // Stores the amount of nickels needed, then subtracts
    		money -= (nickels * 0.05); // that amount in cents from the money total.
    
    		pennies = (money * 100); // Money is in cents, so it is multiplied by 100 to convert
    			               	 // it to the amount of pennies needed.
    
    		std::cout << quarters << " quarters, " << dimes << " dimes, "
    			<< nickels << " nickels, " << " and " << pennies << " pennies.\n";
    	}
    
    	return 0;
    }
    For some reason, it's not calculating the right amount. For example, if I run the program and put in 0.35, when it gets to this part
    Code:
    dimes = (money / 0.1)
    , I know money is equal to 0.1 at that point, but dimes is not being set equal to 1. I put in
    Code:
    std::cout << money;
    just to make sure that money equals 0.1 at that point, which it does. I also tried
    Code:
    std::cout << (money / 0.1)
    to see that money, when it is 0.1, divided by 0.1 does indeed equal 1, which it does. But it won't set dimes equal to 1. If I run the program at 0.36, it calculates everything right. What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    It has to do with the fact that computers cant handle floating point ("real numbers") perfectly. To see this, do:
    Code:
    #include <iostream>
    
    int main(void)
    {
        float foo = 0.1;
    
        if ( foo == 0.1)
            std::cout << "you would think its equal" << std::endl;
        else
            std::cout << "but its not equal" << std::endl;
    }
    Since "money" can be considered a discrete quantity, you could just work with pennies, as "ints", and after you get the money as a float just multiply it by 100, so you only work with pennies (discrete quantities). Later just divide by 100. This will ensure correct comparisons, since your working with ints.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Ok, I tried working with pennies. I did this after getting money as a float from user:
    Code:
    pennies = (money * 100); // stores money as pennies
    It stores the wrong amount sometimes. If I put in 0.35, it stores 34 in pennies. Some numbers work, such as 0.36 or 0.37 (0.38 stores as 37).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So if you do
    Code:
    float f = 0.35;
    int i = 100*f;
    cout << i;
    it prints "34"? Or even by doing "cin >> f" instead of hardcoding it, you get 34?

    EDIT: Compilers are probably a factor too, what are you using?
    Last edited by nadroj; 10-31-2009 at 05:19 PM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Yeah. Using exactly this code:
    Code:
    #include <iostream>
    
    int main(){
    	float f;
    	int i;
    	f = 0.35;
    	i = f * 100;
    	std::cout << i << std::endl;
    
    	return 0;
    }
    I got this:
    34
    Press any key to continue

    I'm using Microsoft Visual Studio .NET 2003.
    Last edited by Fulwin; 10-31-2009 at 05:33 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Try forcing an int by casting i.e.:
    Code:
    i = (int)100*f;
    I get 34 when its interpreted as a float, i.e.:
    Code:
    i = 100.0*f;
    and forcing it to be an int I get 35.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    I tried that, and it still didn't work. However, I was getting the warning "truncation from double to float" on this line
    Code:
    	f = 0.35;
    I tried changing it to a double, and it works now. Given that the double f equals 0.35, (f * 100) stored into an integer gives me 35.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you do "f = (float) 0.35;", and leave f as a float, do you later get integer 35?

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    No, it's still 34. I tried this though
    Code:
    #include <iostream>
    
    int main(){
    	float f;
    	int i;
    	f = 0.35;
    	f *= 100;
    	i = f;
    	std::cout << i << std::endl;
    
    	return 0;
    }
    This prints out 35.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Prefer double over float.
    To create a float, you must do x.yf. Notice the "f" at the end. That will make the compiler treat the number as a float rather than a double.
    Nevertheless, I would still urge you to use integer math.
    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

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