Urgent! C++ project is kicking my butt.

Discussion in 'Programmer's Corner' started by chinesebarbiedoll, Nov 9, 2011.

  1. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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:
    Code ( (Unknown Language)):
    1. #include<iostream>
    2. #include <conio.h>
    3. #include <iomanip> //use for setw
    4. using namespace std;
    5. int main()
    6. {
    7.     int intnum, quarters, dimes, nickels;
    8.     double decnum, money, slot=0.00, coinreturn=0.00;
    9.     bool flag=true;
    10.  
    11.  
    12.     do
    13.     {
    14.          // setprecision(2) so that the 0.00 will appear in the executable.
    15.       cout << setprecision(2) << fixed;      
    16.       slot=0.00;
    17.       coinreturn=0.00;
    18.  
    19.       do
    20.       {
    21.                 system("cls");
    22.          cout<<endl;
    23.          cout << setw(35) << "ICE COLD COLA";
    24.          cout << endl;
    25.          cout << setw(33) << ".75 cents";
    26.          cout << endl;
    27.          cout << endl;
    28.          cout <<"         Deposit -- dollar, quarters, dimes, or nickels:";
    29.          cout << endl;
    30.          cout << setw(22) << "( 1" << "    ," << setw(6)<< " .25" << "   ,"  
    31.               <<  "  .10" << " ," << setw(8) << " .05" << " )";
    32.          cout << endl;
    33.          cout << endl;
    34.          cout << setw(16) << "Slot"<<" [" << slot <<" ]" <<" :";
    35.          cout << endl;
    36.          cout << endl;
    37.          cout << endl;
    38.          cout << endl;
    39.          cout << setw(14) << "Coin Return" <<" [" << coinreturn <<" ]" <<" :";
    40.          cout << endl;
    41.          cout << endl;
    42.          cout << setw(10) << "     Enter Coin" << "[" << "------" <<"]" << " ";
    43.         cin>> money;
    44.         cout<<endl<<endl;
    45.  
    46.         /* makes it so that when .05, .10, .25, and $1.00 is entered into the
    47.            machine the money will be added together and shown in the slot    
    48.            Any amount of money that is not equal to .05, .10, .25, and $1.00
    49.            will be placed in the coin return.
    50.         */
    51.  
    52.         if (money == 1.00 || money == 0.25 || money == 0.10 || money == 0.05)
    53.             slot += money;
    54.         else
    55.             coinreturn += money;
    56.  
    57.  
    58.  
    59.       }while(slot <= .75 && money != -1);
    60.  
    61.        cout<<"   Here is your Cola!"<<endl<<endl<<endl;
    62.        cout<<"   Have a nice day!"<<endl;
    63.  
    64.       decnum = (slot - .75);
    65.  
    66.        //use static_cast to convert a double into an integer
    67.  
    68.        intnum = static_cast<int>((decnum +.005) * 100);
    69.  
    70.  
    71.        quarters = intnum/25;
    72.        intnum = intnum%25;
    73.  
    74.        dimes = intnum/10;
    75.        intnum = intnum%10;
    76.  
    77.        nickels = intnum/5;
    78.        intnum = intnum%5;
    79.  
    80.  
    81.        cout<< endl<< endl<< "  Your change is: " <<endl;
    82.        cout<<"\t"<< quarters <<" Quarter(s)"<<endl;
    83.        cout<<"\t"<< dimes <<" Dime(s)"<<endl;
    84.        cout<<"\t"<< nickels<< " Nickel(s)"<<endl;
    85.  
    86.  
    87.       system("pause");
    88.  
    89.     }while(money != -1);  
    90.  
    91.     cout<<endl<<endl<<endl;
    92.  
    93. }
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    hmm have you done some debugging. Like done some single-stepping on your program, setting Breakpoints and so on.
     
  3. TheFox

    Active Member

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

    New Member

    Mar 8, 2011
    17
    0
    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.
     
  5. Josato

    New Member

    Mar 8, 2011
    17
    0
    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).
     
  6. ho69e3k

    New Member

    Nov 13, 2011
    7
    0
    @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..:
    Code ( (Unknown Language)):
    1.  do
    2.       {
    3.                 system("cls");
    4.          cout<<endl;
    5.          cout << setw(35) << "ICE COLD COLA";
    6.          cout << endl;
    7.          cout << setw(33) << ".75 cents";
    8.          cout << endl;
    9.          cout << endl;
    10.          cout <<"         Deposit -- dollar, quarters, dimes, or nickels:";
    11.          cout << endl;
    12.          cout << setw(22) << "( 1" << "    ," << setw(6)<< " .25" << "   ,"  
    13.               <<  "  .10" << " ," << setw(8) << " .05" << " )";
    14.          cout << endl;
    15.          cout << endl;
    16.          cout << setw(16) << "Slot"<<" [" << slot <<" ]" <<" :";
    17.          cout << endl;
    18.          cout << endl;
    19.          cout << endl;
    20.          cout << endl;
    21.          cout << setw(14) << "Coin Return" <<" [" << coinreturn <<" ]" <<" :";
    22.          cout << endl;
    23.          cout << endl;
    24.          cout << setw(10) << "     Enter Coin" << "[" << "------" <<"]" << " ";
    25.         cin>> money;
    26.         cout<<endl<<endl;
    27.  
    28.         /* makes it so that when .05, .10, .25, and $1.00 is entered into the
    29.            machine the money will be added together and shown in the slot    
    30.            Any amount of money that is not equal to .05, .10, .25, and $1.00
    31.            will be placed in the coin return.
    32.         */
    33.  
    34.         if (money == 1.00 || money == 0.25 || money == 0.10 || money == 0.05)
    35.             slot += money;
    36.         else
    37.             coinreturn += money;
    38.  
    39.  
    40.  
    41.       }while([B]slot <= 1.00[/B] );
    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..,
     
Loading...