Urgent! C++ project is kicking my butt.

Thread Starter

chinesebarbiedoll

Joined Nov 9, 2011
9
I am having problems with my code. For example when I execute my code and type 1 in the one goes to coin return when it should go into the slot. I can't figure out what's wrong with my code. Any help would be extremely appreciated seeing as how the program is due tomorrow.

Here is the code:
Rich (BB code):
#include<iostream>
#include <conio.h>
#include <iomanip> //use for setw
using namespace std;
int main()
{
    int intnum, quarters, dimes, nickels;
    double decnum, money, slot=0.00, coinreturn=0.00;
    bool flag=true;
 
 
    do
    {
         // setprecision(2) so that the 0.00 will appear in the executable.
      cout << setprecision(2) << fixed;      
      slot=0.00;
      coinreturn=0.00;
 
      do
      {
                system("cls");
         cout<<endl;
         cout << setw(35) << "ICE COLD COLA";
         cout << endl;
         cout << setw(33) << ".75 cents";
         cout << endl;
         cout << endl;
         cout <<"         Deposit -- dollar, quarters, dimes, or nickels:";
         cout << endl;
         cout << setw(22) << "( 1" << "    ," << setw(6)<< " .25" << "   ,"   
              <<  "  .10" << " ," << setw(8) << " .05" << " )";
         cout << endl;
         cout << endl;
         cout << setw(16) << "Slot"<<" [" << slot <<" ]" <<" :";
         cout << endl;
         cout << endl;
         cout << endl;
         cout << endl;
         cout << setw(14) << "Coin Return" <<" [" << coinreturn <<" ]" <<" :";
         cout << endl;
         cout << endl;
         cout << setw(10) << "     Enter Coin" << "[" << "------" <<"]" << " ";
        cin>> money;
        cout<<endl<<endl;
 
        /* makes it so that when .05, .10, .25, and $1.00 is entered into the 
           machine the money will be added together and shown in the slot     
           Any amount of money that is not equal to .05, .10, .25, and $1.00
           will be placed in the coin return.
        */
 
        if (money == 1.00 || money == 0.25 || money == 0.10 || money == 0.05)
            slot += money; 
        else
            coinreturn += money;
 
 
 
      }while(slot <= .75 && money != -1);
 
       cout<<"   Here is your Cola!"<<endl<<endl<<endl;
       cout<<"   Have a nice day!"<<endl;
 
      decnum = (slot - .75);
 
       //use static_cast to convert a double into an integer
 
       intnum = static_cast<int>((decnum +.005) * 100);
 
 
       quarters = intnum/25;
       intnum = intnum%25;
 
       dimes = intnum/10;
       intnum = intnum%10;
 
       nickels = intnum/5;
       intnum = intnum%5;
 
 
       cout<< endl<< endl<< "  Your change is: " <<endl;
       cout<<"\t"<< quarters <<" Quarter(s)"<<endl;
       cout<<"\t"<< dimes <<" Dime(s)"<<endl;
       cout<<"\t"<< nickels<< " Nickel(s)"<<endl;
 
 
      system("pause");
 
    }while(money != -1);   
 
    cout<<endl<<endl<<endl;
 
}
 

t06afre

Joined May 11, 2009
5,936
hmm have you done some debugging. Like done some single-stepping on your program, setting Breakpoints and so on.
 

TheFox

Joined Apr 29, 2009
66
If I were you, I'd break down the interface into some functions, so it'll be tidier.
 

Josato

Joined Mar 8, 2011
17
Yes, if I was you, I would also print out the value of 'money' before you compare it, and after you compared it to see where it is gone.

Perhaps there is some problem if you are comparing a double. Also, I would suggest you post your question to stack overflow, they are very helpful and more importantly quick to reply there.

A quick workaround might be to use integers instead of doubles until you figure it out.
 

Josato

Joined Mar 8, 2011
17
Also the thing about floats and doubles is that they store the number quite weirdly by working out the decimal as a fraction (not that you care).
 

ho69e3k

Joined Nov 13, 2011
7
@chinesebarbiedoll
I think the problem here is the conditions in while and if conditions.. If you could only fix them..

Here is the solution to your problem in when you type 1 then goes to slot..:
Rich (BB code):
 do
      {
                system("cls");
         cout<<endl;
         cout << setw(35) << "ICE COLD COLA";
         cout << endl;
         cout << setw(33) << ".75 cents";
         cout << endl;
         cout << endl;
         cout <<"         Deposit -- dollar, quarters, dimes, or nickels:";
         cout << endl;
         cout << setw(22) << "( 1" << "    ," << setw(6)<< " .25" << "   ,"   
              <<  "  .10" << " ," << setw(8) << " .05" << " )";
         cout << endl;
         cout << endl;
         cout << setw(16) << "Slot"<<" [" << slot <<" ]" <<" :";
         cout << endl;
         cout << endl;
         cout << endl;
         cout << endl;
         cout << setw(14) << "Coin Return" <<" [" << coinreturn <<" ]" <<" :";
         cout << endl;
         cout << endl;
         cout << setw(10) << "     Enter Coin" << "[" << "------" <<"]" << " ";
        cin>> money;
        cout<<endl<<endl;
 
        /* makes it so that when .05, .10, .25, and $1.00 is entered into the 
           machine the money will be added together and shown in the slot     
           Any amount of money that is not equal to .05, .10, .25, and $1.00
           will be placed in the coin return.
        */
 
        if (money == 1.00 || money == 0.25 || money == 0.10 || money == 0.05)
            slot += money; 
        else
            coinreturn += money;
 
 
 
      }while(slot <= 1.00 );
Actually another problem occurred in this program when I did it..
I have a change of 5 quarters, 0 dimes and 0 nickels..
But I think you can easily fix the problem..,
 
Top