If I end my cost of .60 cents with a NICKLE or a DIME I get a 2.####.E number.

If I end the total cost with a quarter I don't get a big number ( Wrap around number right ? )

Can anyone tell me why?

#include <cstdlib>
#include <iostream>
char insert;
float cost = .60, change; // cost of soda is .60

using namespace std;

void vendingmachine();
void vendingprocess();

void vendingmachine()
{
                
                cout << "Welcome to the vending machine!\n";
     do{
                cout << "Insert money\n";
                cout << "[n]ickle\n[d]ime\n[q]aurter\n[o]ne dollar\n\n";
                cin >> insert;
                switch(insert)
                {
                              case 'n':
                                       cout << "You insert one nickle into the vending machine.\n";
                                       cost = cost - .05;
                                       cout << "Remaining balance:\t" << cost << endl;
                                       break;
                              case 'd':
                                       cout << "You insert one dime into the vending machine.\n";
                                       cost = cost - .10;
                                       cout << "Remaining balance:\t" << cost << endl;
                                       break;
                              case 'q':
                                       cout << "You insert one quarter into the vending machine.\n";
                                       cost = cost - .25;
                                       cout << "Remaining balance:\t" << cost << endl;
                                       break;
                              case 'o':
                                       cout << "You insert one dollar into the vending machine.\n";
                                       cost = cost - 1.00;
                                       cout << "Remaining balance:\t" << cost << endl;
                 }
      }while ( cost > 0);
}

void vendingprocess()
{
     if ( cost < 0 )
     {
            change = -1 *cost;
            cout << "Your change is :\t"<<change<<endl;
            cout << "Please take your drink!\n";
     }
}



int main()
{
    vendingmachine();
    vendingprocess();
    
    
    system("pause");   // I'm using system pause because cin.get() exits the program right away!
    return EXIT_SUCCESS;
}

Thanks,
Derek

floating point numbers tend not to be very exact. What is happening is after subtracting a total of 60 cents from your original 60 cents, you're getting a number very close to (but not exactly) zero, due to the nature of floats.

In situations like this, you should try to avoid floating point numbers if you can. Instead, use an integer to keep track of the number of cents in the cost. It will take slightly more work to output the amount, but you can be sure your arithmetic is all exact and you won't run into problems like this.

Integers don't do decimals I want to do decimals for cents....unless I use int and divide?

There is no need to use decimals at all if you use integers to keep track of the number of cents instead of the number of dollars. Using floats (or even doubles for that matter) can lead you into problems when using them in this way, as you have seen first-hand.

If you still want to output the number of dollars in $X.YY format, you don't need floating point numbers to be able to do so. For example,
Instead of:

cout << "Remaining balance:\t" << cost << endl;

You would have:

cout << "Remaining balance:\t" << cost/100 << "." << cost%100 << endl;

As a side note, lines 24, 29, 34 and 39 of your code are all identical, so there's no sense in having them inside the case structure, as that line will be executed in all cases. You might as well just move them outside of the switch, so the line only appears once.

Well that works. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.