Thread: Help coin program.

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Help coin program.

    Well the prof describes it as a vending machine program but there are no menus, the user just enter an amount that is multiple of 5 and i'm supposed to display the change due in terms of quarters, dimes, etc. The problem i have is that the machine is initially filled with 2 quarters, 4 dimes, and 4 nickels, and once there are no more quarters i should be able to give back change in dimes and nickels as long as there is enough change for it and when i tried test cases after no quarters were left it will give more quarters as change. I will appreciate if someone points out what I'm doing wrong and here is my current code(lil messy, trying to make it work before clean up).

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    int const QUARTER = 25;
    int const NICKEL = 5;
    int const DIME = 10;
    
    int totalQuarters, totalNickels, totalDimes,q,n,d;
    int currentBalance,amount, change,change2,remainder,changeleft;
    double totalBalance;
    
    ifstream coinIn;
    cout<<"Vending Machine\n\n";
    cout<<"Filling machine with change. Please wait....\n";
    coinIn.open("coin.dat");
    coinIn >> totalQuarters>>totalDimes>>totalNickels;
    cout<<"There are "<<totalQuarters<<" quaters, "<<totalDimes<<" Dimes,and "<<totalNickels<<" Nickels."<<endl;
    
    currentBalance = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
    cout<<fixed<<setprecision(2)<<showpoint;
    cout<<"Initial machine balance is $"<<currentBalance/100.00<<endl;
    
    
    cout<<"Enter a purchase amount [0-100] -->";
    cin>>amount;
    changeleft = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
    
    if (amount >= 0 && amount <= 100)
    {
    
    while (amount != 0)
    {
    change = 100-amount;
    cout<<"\n\nYou entered a purchase amount of "<<amount<<" cents."<<endl;
    cout<<"Please enter one-dollar bill"<<endl;
    cout<<"Processing your purchase"<<endl;
    cout<<"Thank you for your purchase"<<endl;
    
    change2 = change;
    if( totalQuarters != 0 && change2 < changeleft)
    {
    q = change2/QUARTER;
    remainder = change2%QUARTER;
    totalQuarters -= q;
    d = remainder / DIME;
    remainder = remainder%DIME;
    totalDimes -= d;
    n = remainder / NICKEL;
    remainder = remainder%NICKEL;
    totalNickels -=n;
    
    }
    else if (totalDimes != 0 && change2 < changeleft)
    {
    d = change2/ DIME;
    remainder = remainder%DIME;
    totalDimes -= d;
    n = remainder / NICKEL;
    remainder = remainder%NICKEL;
    totalNickels -=n;
    
    }
    else if (totalNickels != 0 && change2 < changeleft)
    {
    n = change2 / NICKEL;
    remainder = remainder%NICKEL;
    totalNickels -=n;
    }
    else
    {
    cout<<"Insufficient change!\n";
    cout<<"Your transaction cannot be processed.\n";
    cout<<"Please take back your dollar bill.\n";
    cout<<"\n\nEnter a purchase amount [0-100] -->";
    cin>>amount;
    }
    changeleft = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
    
    totalBalance = (changeleft + amount)/100.00;
    
    cout<<"Your change of "<<change<<" is given as:"<<endl;
    cout<<q<<" quaters, "<<d<<" Dimes, and "<<n<<" Nickels ("<<change<<" cents.)"<<endl;
    cout<<"\n\nEnter a purchase amount [0-100] -->";
    cin>>amount;
    }
    }
    else
    {
    cout<<"Invalid amount entered, please enter a valid amount\n";
    }
    
    cout<<"Output file is being generated....\n";
    cout<<"Machine is shutting down. Good bye.\n";
    
    ofstream balance;
    balance.open("machine.txt");
    balance<<"Number of quarters: "<<totalQuarters<<endl;
    balance<<"Number of dimes: "<<totalDimes<<endl;
    balance<<"Number of nickels: "<<totalNickels<<endl;
    balance<<fixed<<setprecision(2)<<showpoint;
    balance<<"Current balance: $"<<totalBalance<<endl;
    
    
    
    
    coinIn.close();
    
    return 0;
    }
    In some cases the result is correct like the attachment screen shows, but in many others i get quarters when I was not supposed to.
    Last edited by Salem; 09-12-2009 at 11:22 PM. Reason: Restored OP - Don't edit your post to nothing just because you got your answer - no one else can learn if you do that

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your screenshot doesn't show an error. Surprisingly, you never check to see if you ever go negative (i.e., if you have 1 quarter, you will cheerfully dispense three).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 02:02 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM