Jump to content

Why won't my program work?!

- - - - -

  • Please log in to reply
4 replies to this topic

#1
chinesebarbiedoll

chinesebarbiedoll

    Newbie

  • Members
  • Pip
  • 2 posts
Everytime I enter 1 it won't into the slot, and when I enter 1.50 it goes to coinreturn. what am I doing wrong?

Heres the 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;
    
}

Edited by WingedPanther, 09 November 2011 - 07:31 PM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Using floats for money is generally a bad idea. 1.00 may be stored internally as 0.999999999998, and fail your check.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
chinesebarbiedoll

chinesebarbiedoll

    Newbie

  • Members
  • Pip
  • 2 posts

WingedPanther said:

Using floats for money is generally a bad idea. 1.00 may be stored internally as 0.999999999998, and fail your check.
Alright I changed the 1.00 to 1. But i am still stumped on why the program isn't working right.

#4
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
I guess you didn't paid attention to review your code!
 

      quarters = intnum/25;

       intnum = intnum%25;

 

In the second statement, you put semicolon in between 37 and 25 -- the statement is terminating by the end of 37. Well! And the operators &#, you might know better why you have used these even you were getting error...

Fix it. It must be the way you want it.:P

Hope this helps!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

chinesebarbiedoll said:

Alright I changed the 1.00 to 1. But i am still stumped on why the program isn't working right.
You don't understand, the VARIABLE is not 1.00. What you should do is multiply by 100 and round, storing the result in an int. That way, you aren't losing precision. So $1.00 would be worked with as 100., $.25 would be worked with as 25, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users