C homework

This is a discussion on C homework within the C Programming forums, part of the General Programming Boards category; Hello- I'm still fresh to programming in general and am hung up on a project for my C class. I ...

  1. #1
    Registered User wilson5182004's Avatar
    Join Date
    Feb 2009
    Posts
    2

    Talking C homework

    Hello-
    I'm still fresh to programming in general and am hung up on a project for my C class. I would love any input i could get on it.

    The program is a vending machine. The portion i'm stuck on is the 'make change' aspect. My machine needs to be able to not just give the user change but gibe back the smallest amount of change in dollars, quarters, nickels dimes... that's the part i'm stuck on. My weak point is loops which i feel like this needs. I also tried using a nested if/then.....

    see my code (a little messy as its a work in process):

    Code:
    #include<stdio.h>
    
    main()
    {
    
    float fltItem = 0.0;
    float fltUserMoney = 0.0;
    float fltChange = 0.0;
    int intSelection = 0;
    int intDollar = 0;
    int intQuarter= 0;
    int intDime = 0;
    int intNickel = 0;
    int count=0;
    
    printf("\nInput Money Now: ");
    scanf("%f", &fltUserMoney);
    printf("\n");
    printf("\tPlease Select Your Item\n");
    printf("\n");
    printf("\n1- Gum .20");
    printf("\n2- Peanuts .35");
    printf("\n3- Candy Bar .55");
    printf("\n4- Soda 1.50");
    printf("\n5- Chips .70");
    printf("\n6- Crackers .65");
    printf("\n7- Water 1.20");
    printf("\n8- Fruit Bar .80");
    
    printf("\nPlease Make a Selection: ");
    scanf("%d", &intSelection);
    
    switch (intSelection)
    	{
    
    case 1: fltItem = 0.20;
    break;
    
    case 2: fltItem = 0.35;
    break;
    
    case 3: fltItem = 0.55;
    break;
    
    case 4: fltItem = 1.50;
    break;
    
    case 5: fltItem = 0.70;
    break;
    
    case 6: fltItem = 0.65;
    break;
    
    case 7: fltItem = 1.20;
    break;
    
    case 8: fltItem = 0.80;
    break;
    
    default:
    printf("\n Please make a valid selection.");
    break;
    	}
    
    printf("\nThe Total is $%.2f", fltItem);
    
    //fltChange = (fltItem-fltUserMoney);
    //	
    //	if (fltChange >.99)
    //		intDollar++;
    //	else intQuarter++;
    //
    			
    //printf("%d, %d", intDollar, intQuarter);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wilson5182004 View Post
    My machine needs to be able to not just give the user change but gibe back the smallest amount of change in dollars, quarters, nickels dimes... that's the part i'm stuck on.
    Oh come on -- just divide by the higher denominations first (the same way you would at a cash register, or using your wallet).

    If you have a specific problem with a method (nested loops or whatever), come up with a simple experiment as a seperate program to test this method, rather than fooling around in a longer piece of code with lots of irrelevant detail. This also makes it easier to ask straightforward questions to which you will get quicker replies, rather than getting into the habit of posting your entire assignment and asking what's wrong with it (tho you can do that if you want).

    Some additional bad news that I must bear: there is a problem using floating point numbers for money in C, and that is that money does not use floating point numbers, it uses fixed precision numbers. It will seem to work fine until you hit some magic combination of bits (they could differ slightly from here to there) such that adding 0.65 to 0.30 may not equal 0.95. There is a good complete explanation of why somewhere around here...it is not as simple as a matter of rounding up or down (what's to round there?) but to do with binary representation.

    The normative, safe way to deal with money is use integers (cents) rather than dollars and then only convert to floating point when you need to output a "$5.25". Just remember if you do division, C normally rounds down (so 9.9 is 9).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Something along the line of
    Code:
    while (price >= 1) {
    //give a dollar
    price -= 1;
    }
    while (price >= 0.25) {
    //give a quarter
    price -= 0.25;
    }
    ...

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by MK27 View Post
    Just remember if you do division, C normally rounds down (so 9.9 is 9).
    It truncates, it doesn't round down. The value -9.9 would become -9, which is actually rounding up in the case of negative values.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by MK27 View Post
    Some additional bad news that I must bear: there is a problem using floating point numbers for money in C, and that is that money does not use floating point numbers, it uses fixed precision numbers. It will seem to work fine until you hit some magic combination of bits (they could differ slightly from here to there) such that adding 0.65 to 0.30 may not equal 0.95. There is a good complete explanation of why somewhere around here...it is not as simple as a matter of rounding up or down (what's to round there?) but to do with binary representation.

    The normative, safe way to deal with money is use integers (cents) rather than dollars and then only convert to floating point when you need to output a "$5.25". Just remember if you do division, C normally rounds down (so 9.9 is 9).
    Yes but I don't see anything in wilson5182004's current program that warrants having to switch to a different data type yet, and it can be done okay using floats.
    Don't try and throw too much at every beginner. If they stick with programming for a while they'll eventually come across the realisation that even 0.1 + 0.9 does not equal 1.0 exactly (that's the very thing that first made me realise that floating point was not perfect, all those years ago)
    Last edited by iMalc; 03-01-2009 at 12:37 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User wilson5182004's Avatar
    Join Date
    Feb 2009
    Posts
    2

    Lightbulb Thanks!

    To everyone, I pulled together a while loop (well i started with one, went to a do/while, then back to the while), and am actually happy to say I think i've finally wrapped my head around looping.

    The truncating floating points isn't so much an issue with this program (beginner course project...), but it is one that got brought up in class. Is there a way around that....?

    For now, i'm one happy girl with a finished project(well mostly, i'm going to try and add more neat stuff to it). Here's my while:

    Code:
    if (fltUserMoney >= fltItem)
    	{
    	while (fltChange >= 1.00)
    	{
    	fltChange -= 1.00;
    	intDollar++;	
    	}
    	while (fltChange >= 0.25)
    	{
    	fltChange -= 0.25;
    	intQuarter++;
    	}
    	
    	while (fltChange >= 0.10)
    	{
    	fltChange -= 0.10;
    	intDime++;
    	}
    	
    	while (fltChange >= 0.05)
    	{
    	fltChange -= 0.05;
    	intNickel++;
    	}
    	printf("\nYour Change is $%.2f, you get %d dollars , %d quarters, %d dimes, %d nickels", fltViewChange, 	
    
    	intDollar, intQuarter, intDime, intNickel);
    	}
    
    	else {
    	printf("Please try again, enter the correct amount of funds next time.");
    	}

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by wilson5182004
    For now, i'm one happy girl with a finished project(well mostly, i'm going to try and add more neat stuff to it). Here's my while:
    That is good, but you would find it easier to read, trace and understand your code if you indented it consistently, e.g.,
    Code:
    if (fltUserMoney >= fltItem)
    {
        while (fltChange >= 1.00)
        {
            fltChange -= 1.00;
            intDollar++;	
        }
    
        while (fltChange >= 0.25)
        {
            fltChange -= 0.25;
            intQuarter++;
        }
    
        while (fltChange >= 0.10)
        {
            fltChange -= 0.10;
            intDime++;
        }
    
        while (fltChange >= 0.05)
        {
            fltChange -= 0.05;
            intNickel++;
        }
    
        printf("\nYour Change is $%.2f, you get %d dollars , %d quarters, %d dimes, %d nickels",
            fltViewChange, intDollar, intQuarter, intDime, intNickel);
    }
    else
    {
        printf("Please try again, enter the correct amount of funds next time.");
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 05:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 04:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 12:48 PM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 02:26 PM

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