Strange Program Output

I can't seem to find out what's got my program output going crazy. Any ideas?

Assignment and Code:
/*
14. Drink Machine Simulator
Create a class that simulates and manages a soft drink machine. Information on each drink
type should be stored in a structure that has data members to hold the drink name, the
drink price, and the number of drinks of that type currently in the machine.
The class should have an array of ?ve of these structures, initialized with the following data.


Drink Name Cost Number in Machine
Cola 1.00 20
Root beer 1.00 20
Orange soda 1.00 20
Grape soda 1.00 20
Bottled water 1.50 20


The class should have two public member functions, displayChoices (which displays a
menu of drink names and prices) and buyDrink (which handles a sale). The class should
also have at least two private member functions, inputMoney, which is called by buyDrink
to accept, validate, and return (to buyDrink) the amount of money input, and
dailyReport, which is called by the destructor to report how many of each drink type
remain in the machine at the end of the day and how much money was collected.


You may want to use additional functions to make the program more modular.
The client program that uses the class should have a main processing loop which calls the
displayChoices class member function and allows the patron to either pick a drink or
quit the program. If the patron selects a drink, the buyDrink class member function is
called to handle the actual sale. This function should be passed the patron’s drink choice.




Here is what the buyDrink function should do:
• Call the inputMoney function, passing it the patron’s drink choice.
• If the patron no longer wishes to make the purchase, return all input money.
• If the machine is out of the requested soda, display an appropriate “sold out” message and return all input money.
• If the machine has the soda and enough money was entered, complete the sale by updating the quantity on hand and money collected information, calculating any change due to be returned to the patron, and delivering the soda.


This last action can be simulated by printing an appropriate “here is your beverage” message.
Input Validation: Only accept valid menu choices. Do not deliver a beverage if the
money inserted is less than the price of the selected drink.
*/


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cctype>


using namespace std;


struct Array
{
string sodaName;
double price;
int quantity;
};




class Vending
{
private:
Array soda[5];
double cashInput;
double change;
string lastChance;


public:
void setPrice()
{
for (int i = 0; i < 5; i++)
{
if (i == 4)
{
soda[i].price = 1.50;
}
else
{
soda[i].price = 1.00;
}
soda[i].quantity = 20;
}


soda[0].sodaName = "Cola";
soda[1].sodaName = "Root Beer";
soda[2].sodaName = "Orange Soda";
soda[3].sodaName = "Grape Soda";
soda[4].sodaName = "Bottled Water";
}


void displayChoices()
{
for (int i = 0; i < 5; i++)
{
cout << i + 1 << "Soda Name: " << soda[i].sodaName << " Cost: $" << soda[i].price << endl;
}
}


void purchase(int choice)
{
if (soda[choice - 1].quantity <= 0)
{
cout << "I am sorry, but we are out of that drink. Please purchase a different drink.";
}
else
{
cout << "The price of " << soda[choice - 1].sodaName << " is: $" << soda[choice - 1].price << endl;


cout << "Are you sure that you would like to make this purchase? (y/n)";
cin >> lastChance;


if (lastChance == "Y" || lastChance == "y")
{
cout << "Please input money into the machine: ";
cin >> cashInput;


change = (cashInput - soda[choice - 1].price);


if (soda[choice - 1].price < cashInput)
{
cout << "Improper funds. Returning your input amount of $: " << cashInput;
}
else
{
cout << "Thank you. Please take your drink." << endl;
cout << "Your change return: " << change << endl;
}
}
else if (lastChance == "N" || lastChance == "n")
{
cout << "Thank you please come again";
}
}
}
};


int main()
{
int choice;
Vending machine;


cout << "Pleae enter a number (1-5) to make your choice" << endl;
machine.displayChoices();
cin >> choice;


if (choice < 1 || choice > 5)
{
cout << "Not an option. Please enter a proper choice (1-5)." << endl;
return 0;
}
else
{
switch (choice)
{
case 1: machine.purchase(choice);
break;


case 2: machine.purchase(choice);
break;


case 3: machine.purchase(choice);
break;


case 4: machine.purchase(choice);
break;


case 5: machine.purchase(choice);
break;
}
}


return 0;
}
I think the problem is the price for each drink never gets set. I had the following output on my machine
1
2
3
4
5
6
7
Pleae enter a number (1-5) to make your choice
1Soda Name:  Cost: $0
2Soda Name:  Cost: $6.9335e-310
3Soda Name:  Cost: $3.11253e-317
4Soda Name:  Cost: $3.11253e-317
5Soda Name:  Cost: $0

You should call setPrice in your Vending's constructor like so:
1
2
3
4
Vending(){
    // ... blah blah
    setPrice();
}


You are also trying to compare two doubles on this line
 
if (soda[choice - 1].price < cashInput)


It is better to use an epsilon value to because of the way most cpu's represent floating point values.

I hope this helps
Last edited on
Topic archived. No new replies allowed.