Thread: Problem with while cycle

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    Problem with while cycle

    Here it is the code:

    Code:
    #include <stdio.h>
    
    int main() {
    
    	float cafe=0.27, troco, d_int=2.00;
    	troco=d_int-cafe;
    
    	printf("Moedas do troco:\n");
    
    	while (troco>0) {
    
    		if ((troco/2.00)>=1.00) {
    			printf("1x 2 euros\n");
    			troco-=2.00;
    		}
    		else if ((troco/1.00)>=1.00) {
    				printf("1x 1 euro\n");
    				troco-=1.00;
    			}
    			else if ((troco/0.50)>=1.00) {
    					printf("1x 50 cêntimos\n");
    					troco-=0.50;
    				}
    				else if ((troco/0.20)>=1.00) {
    					printf("1x 20 cêntimos\n");
    					troco-=0.20;
    					}
    					else if ((troco/0.10)>=1.00) {
    						printf("1x 10 cêntimos\n");
    						troco-=0.10;
    						}
    						else if ((troco/0.05)>=1.00) {
    							printf("1x 5 cêntimos\n");
    							troco-=0.05;
    							}
    							else if ((troco/0.02)>=1.00) {
    								printf("1x 2 cêntimos\n");
    								troco-=0.02;
    								}
    								else if ((troco/0.01)>=1.00) {
    									printf("1x 1 cêntimo\n");
    									troco-=0.01;
    									}
    	}
    	return 0;
    }
    The variable "troco" reaches 0.00, however the while cycle doesn't break on "troco>0" condition, so it enters in an infinite cycle. I can't tell why.

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Here is your program with some printf statements that will output what troco is holding at each point in your program. Take a look at the values you are getting when you run it and see if you can figure anything out by this. This is called debugging, and as much as it can be a pain it will help you learn a lot.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
    	float cafe=0.27, troco, d_int=2.00;
    	troco=d_int-cafe;
    
    	printf("Moedas do troco:\n");
    
    	while (troco > 0) {
    
    		if ((troco/2.00)>=1.00) {
    			printf("1x 2 euros\n");
    			printf("%d\n", troco);
    			troco-=2.00;
    		}
    		else if ((troco/1.00)>=1.00) {
    				printf("1x 1 euro\n");
    				printf("%d\n", troco);
    				troco-=1.00;
    			}
    			else if ((troco/0.50)>=1.00) {
    					printf("1x 50 cêntimos\n");
    					printf("%d\n", troco);
    					troco-=0.50;
    				}
    				else if ((troco/0.20)>=1.00) {
    					printf("1x 20 cêntimos\n");
    					printf("%d\n", troco);
    					troco-=0.20;
    					}
    					else if ((troco/0.10)>=1.00) {
    						printf("1x 10 cêntimos\n");
    						printf("%d\n", troco);
    						troco-=0.10;
    						}
    						else if ((troco/0.05)>=1.00) {
    							printf("1x 5 cêntimos\n");
    							printf("%d\n", troco);
    							troco-=0.05;
    							}
    							else if ((troco/0.02)>=1.00) {
    								printf("1x 2 cêntimos\n");
    								printf("%d\n", troco);
    								troco-=0.02;
    								}
    								else if ((troco/0.01)>=1.00) {
    									printf("1x 1 cêntimo\n");
    									printf("%d\n", troco);
    									troco-=0.01;
    									}
    	}
    	system("PAUSE");
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Using a non-integral value to control a while or for loop is dangerous. Your problem is that troco doesn't always reach exactly 0. In the floating-point representation it may reach something like 0.0000000011 which you see as 0 but is still greater than 0 for purposes of the boolean comparison.

    Instead, try using a value that's small enough for purposes of your application but large enough to rule out tiny round-off differences. For example

    Code:
    while (troco > 0.000001) {

  4. #4
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    I did that, however I did not paste it here.

    The last printf() gives troco=0. That's why I don't get it...

  5. #5
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    Quote Originally Posted by R.Stiltskin View Post
    Using a non-integral value to control a while or for loop is dangerous. Your problem is that troco doesn't always reach exactly 0. In the floating-point representation it may reach something like 0.0000000011 which you see as 0 but is still greater than 0 for purposes of the boolean comparison.

    Instead, try using a value that's small enough for purposes of your application but large enough to rule out tiny round-off differences. For example

    Code:
    while (troco > 0.000001) {
    Oh, that's right, it worked. Thanks!

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    By the way, the reason your printf statements didn't reveal this is that you were printing troco as an integer ( "%d" ) or as a float but with only a few decimal places. Try printing it as a floating-point value, and specify the decimal precision, e.g.
    Code:
    printf("%.32f\n", troco);
    and you will see more of what's actually there.

Popular pages Recent additions subscribe to a feed

Similar Threads

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