Thread: I need help with the conditions, my code just freezes...

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    5

    I need help with the conditions, my code just freezes...

    My code runs fine until i get to the conditions... which is practically all of this part. i don't know what is making it freeze. I've also tried to make all the ifs into whiles, same result. i am guessing it;s my conditions but im stuck

    Code:
    float Change, Denomination, PurchasePrice;
    
    
    void main()
    {
        int thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent;
        thou = fhun = thun = hun = fty = tty = ten = five = one = tfcent = 0;
        Denomination = 1000;
        PurchasePrice = 123.4;
        
        Change = Denomination - PurchasePrice;
        if(Change < '0')
        {
            printf("PHP%.2f is not enough to pay for PHP%d item.", Denomination, PurchasePrice);
        }
        else
        {
            while (Change >= '0')
            {
                if (Change/1000 >= '1')
                {
                    thou++;
                    Change = Change - 1000;
                }
                else if ((Change/500) >= '1')
                {
                    fhun++;
                    Change = Change - 500;
                }
                else if ((Change/200) >= '1')
                {
                    thun++;
                    Change = Change - 200;
                }
                else if ((Change/100) >= '1')
                {
                    hun++;
                    Change = Change - 100;
                }
                else if ((Change/50) >= '1')
                {
                    fty++;
                    Change = Change - 50;
                }
                else if ((Change/20) >= '1')
                {
                    tty++;
                    Change = Change - 20;
                }
                else if ((Change/10) >= '1')
                {
                    ten++;
                    Change = Change - 10;
                }
                else if ((Change/5) >= '1')
                {
                    five++;
                    Change = Change - 5;
                }
                else if ((Change/1) >= '1')
                {
                    one++;
                    Change = Change - 1;
                }
                else if (Change >= '.25')
                {
                    tfcent++;
                    Change = Change - .25;
                }
            }
            printf("The change is PHP%.2f in\n%d thousand bills\n%d five hundred bills\n%d two hundred bills\n%d one hundred bills\n%d fifty bills\n%d twenty coins\n%d ten coins\n%d five coins\n%d one coins\n%d twentyfive cent coins", Change, thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent);
    //        return Change;
        }
    }
    Last edited by 14103959; 12-02-2020 at 11:59 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    > void main()
    main returns an int.

    > if(Change < '0')
    You probably want to remove the single quotes from every one of your comparisons.

    '0' == 48 (in ASCII)

    So yeah, it's a valid test, but it's not what you meant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    I did it but... still black command prompt...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    Yeah, you need to show your edits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    Code:
    float Change, Denomination, PurchasePrice;
    
    
    int main()
    {
    	int thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent;
    	thou = fhun = thun = hun = fty = tty = ten = five = one = tfcent = 0;
    	Denomination = 1000;
    	PurchasePrice = 123.4;
    	
    	Change = Denomination - PurchasePrice;
    	if(Change < 0)
    	{
    		printf("PHP%.2f is not enough to pay for PHP%.2f item.", Denomination, PurchasePrice);
    	}
    	else
    	{
    		while (Change >= 0)
    		{
    			if (Change/1000 >= 1)
    			{
    				thou++;
    				Change = Change - 1000;
    			}
    			else if ((Change/500) >= 1)
    			{
    				fhun++;
    				Change = Change - 500;
    			}
    			else if ((Change/200) >= 1)
    			{
    				thun++;
    				Change = Change - 200;
    			}
    			else if ((Change/100) >= 1)
    			{
    				hun++;
    				Change = Change - 100;
    			}
    			else if ((Change/50) >= 1)
    			{
    				fty++;
    				Change = Change - 50;
    			}
    			else if ((Change/20) >= 1)
    			{
    				tty++;
    				Change = Change - 20;
    			}
    			else if ((Change/10) >= 1)
    			{
    				ten++;
    				Change = Change - 10;
    			}
    			else if ((Change/5) >= 1)
    			{
    				five++;
    				Change = Change - 5;
    			}
    			else if ((Change/1) >= 1)
    			{
    				one++;
    				Change = Change - 1;
    			}
    			else if (Change >= .25)
    			{
    				tfcent++;
    				Change = Change - .25;
    			}
    		}
    		printf("The change is PHP%.2f in\n%d thousand bills\n%d five hundred bills\n%d two hundred bills\n%d one hundred bills\n%d fifty bills\n%d twenty coins\n%d ten coins\n%d five coins\n%d one coins\n%d twentyfive cent coins", Change, thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent);
    //		return Change;
    	}
    }

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    167
    Here's an endless pile of 0.25 coins.

    Can you give 0.40 in change?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    Code:
    $ gcc -Wall -g foo.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) run
    Starting program: a.out 
    ^C
    Program received signal SIGINT, Interrupt.
    0x000000000040075b in main () at foo.c:46
    46	            else if ((Change/20) >= 1)
    (gdb) print Change
    $1 = 0.0999755859
    (gdb) print Denomination
    $2 = 1000
    (gdb) print PurchasePrice 
    $3 = 123.400002
    (gdb) info locals
    thou = 0
    fhun = 1
    thun = 1
    hun = 1
    fty = 1
    tty = 1
    ten = 0
    five = 1
    one = 1
    tfcent = 2
    (gdb)
    Floating point numbers are useless for money.

    Most of the fractions you end up some +/- 0.00000001 away from what you expected.
    So after a long series of calculations, the accumulated error is far from the zero you expected.

    Try it with integer cents.
    Code:
        int Denomination = 100000; // aka 1000.00
        int PurchasePrice = 12340;  // aka 123.40
         
        int Change = Denomination - PurchasePrice;
        if(Change < 0)
        {
            int d1 = Denomination / 100;
            int d2 = Denomination % 100;
            int p1 = PurchasePrice / 100;
            int p2 = PurchasePrice % 100;
            printf("PHP%d.%02d is not enough to pay for PHP%d.%02d item.", d1, d2, p1, p2);
        }
        else
        {
            while (Change >= 0)
            {
                if (Change/100000 >= 1)
                {
                    thou++;
                    Change = Change - 100000;
                }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    Where do you run the code? my windows command prompt bricks on the start of this function.
    Would you mind if i posted the whole code?

    Code:
    int thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent, ChangeDecimal;
        thou = fhun = thun = hun = fty = tty = ten = five = one = tfcent = 0;
        
        int c1 = Change / 100;
        int c2 = Change % 100;
        
        Change = Denomination - PurchasePrice;
    
    
        if(Change < 0)
        {
        int d1 = Denomination / 100;
        int d2 = Denomination % 100;
        int p1 = PurchasePrice / 100;
        int p2 = PurchasePrice % 100;
        printf("PHP%d.%02d is not enough to pay for PHP%d.%02d item.", d1, d2, p1, p2);
        }
        else
        {
            while (Change >= 0)
            {
                if (Change/100000 >= 1)
                {
                    thou++;
                    Change = Change - 100000;
                }
                else if ((Change/50000) >= 1)
                {
                    fhun++;
                    Change = Change - 50000;
                }
                else if ((Change/20000) >= 1)
                {
                    thun++;
                    Change = Change - 20000;
                }
                else if ((Change/10000) >= 1)
                {
                    hun++;
                    Change = Change - 10000;
                }
                else if ((Change/5000) >= 1)
                {
                    fty++;
                    Change = Change - 5000;
                }
                else if ((Change/2000) >= 1)
                {
                    tty++;
                    Change = Change - 2000;
                }
                else if ((Change/1000) >= 1)
                {
                    ten++;
                    Change = Change - 1000;
                }
                else if ((Change/500) >= 1)
                {
                    five++;
                    Change = Change - 500;
                }
                else if ((Change/100) >= 1)
                {
                    one++;
                    Change = Change - 100;
                }
                else if (Change >= 25)
                {
                    tfcent++;
                    Change = Change - 25;
                }
            }
            
    
    
            printf("The change is PHP%d.%02d in\n%d thousand bills\n%d five hundred bills\n%d two hundred bills\n%d one hundred bills\n%d fifty bills\n%d twenty coins\n%d ten coins\n%d five coins\n%d one coins\n%d twentyfive cent coins", c1, c2, thou, fhun, thun, hun, fty, tty, ten, five, one, tfcent);
    //        return Change;
    Last edited by 14103959; 12-03-2020 at 02:10 AM.

  9. #9
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    nope, only .25 coins

  10. #10
    Registered User
    Join Date
    Sep 2020
    Posts
    167
    So your while loop will never exit because you are testing against 0, not 0.2499999999....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-24-2014, 11:47 AM
  2. C - Program freezes; futex_wait_queue_me
    By youjustreadthis in forum C Programming
    Replies: 0
    Last Post: 08-22-2012, 09:49 AM
  3. Compiles fine, freezes when Run?
    By Mahdi123 in forum C Programming
    Replies: 7
    Last Post: 04-10-2007, 12:36 PM
  4. screensaver freezes
    By MisterSako in forum Tech Board
    Replies: 4
    Last Post: 06-03-2005, 07:43 PM
  5. VC++ 6.0 on XP ~ When Compiling, it freezes...
    By Darkman in forum Windows Programming
    Replies: 3
    Last Post: 01-08-2004, 01:43 PM

Tags for this Thread