Array and class - Can't add numeric values

So, I'm writing the program to be a simulated soda machine, but I can't get a "SOLD OUT" to display when the count reaches zero and I can't get the $1.50 soda total purchases to add correctly.

Thank you to all that read my code and help!

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
struct Drink
{
Drink(string n, double cs, int cn)
{
name = n;
cost = cs;
count = cn;
}
Drink()
{
name = " ";
cost = 0;
count = 0;
}

string name;
double cost;
int count;

int displayChoices();
double buyDrink(int c);
double inputMoney(double c);
int dailyReport(double m, int i);
};

int Drink::displayChoices()
{
int choice;
cout << "\n" << setw(7) << "Item" << setw(16) << "Cost\n";
cout << "1. Coca Cola 1.00\n";
cout << "2. Root Beer 1.00\n";
cout << "3. Orange Soda 1.00\n";
cout << "4. Grape Soda 1.00\n";
cout << "5. Bottled Water 1.50\n";
cout << "\nEnter your beverage choice: ";
cin >> choice;

while (choice < 1 || choice > 5)
{
cout << "Please select items 1-5";
cout << "\nRe-enter: ";
cin >> choice;
}
return choice;
}

double Drink::buyDrink(int choice)
{
string selection;
switch (choice)
{
case 1:
selection = "Coca Cola";
break;
case 2:
selection = "Root Beer";
break;
case 3:
selection = "Orange Soda";
break;
case 4:
selection = "Grape Soda";
break;
case 5:
selection = "Bottled Water";
break;
}
cout << "You selected " << selection << ".\n";

double cost;
double change;
if (choice >= 1 && choice <= 4)
{ cost = 1.00;
change = inputMoney(cost);
return change;
}
if (choice = 5)
{ cost = 1.50;
change = inputMoney(cost);
return change;
}
}

double Drink::inputMoney(double cost)
{ double money = 0;
if (cost == 1.00)
{ cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
cin >> money;
while (money < cost)
{ cout << "Please enter the correct change.\n";
cin >> money;}
if (money > cost)
{ money = money - cost;
cout << "Your change is $" << money << "\n";
}
else if (money == 0 && money < cost)
{money = 0;
cout << "Purchase canceled.\n";
}
return money;
}

else if (cost == 1.50)
{ cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
cin >> money;
while (money < cost && money !=0)
{ cout << "Please enter the correct change.\n";
cin >> money;}
if (money > cost && money !=0)
{ money = money - cost;
cout << "Your change is $" << money << "\n";
}
else if (money == 0 && money < cost)
{money = 0;
cout << "Purchase canceled.\n";
}
return money;
}
}

int Drink::dailyReport(double money, int count)
{
if (money > 0)
{
cout << "*****Dispensing*****\n";
cout << "Here is your beverage! Enjoy!\n";
count--;
return count--;
}
else
{
cout << "Come back next time!\n";
return count;
}

}



int main()
{
Drink vending;
int choice;
int continuing;
double change;
const int DRINKS = 5;
Drink data[DRINKS] = {Drink("Coca Cola", 1.00, 20),
Drink("Root Beer", 1.00, 20),
Drink("Orange Soda", 1.00, 20),
Drink("Grape Soda", 1.00, 20),
Drink("Bottled Water", 1.50, 20)};
cout << "Welcome to the Drink Machine!\n";
cout << "Press 1 to enter, 0 to stop.";
cin >> continuing;

while (continuing != 0)
{
choice = vending.displayChoices();
change = vending.buyDrink(choice);

switch (choice)
{
case 1:

if (data[0].count > 0)
data[0].count = vending.dailyReport(change, data[0].count);
cout <<"\n";
cout << data[0].name << " Status:" <<endl;
cout << "$" << data[0].cost++ << " deposited, ";
cout << data[0].count <<" remain for purchase." << endl;
break;

case 2:

if (data[1].count > 0)
data[1].count = vending.dailyReport(change, data[1].count);
cout <<"\n";
cout << data[1].name << " Status:" <<endl;
cout << "$" << data[1].cost++ << " deposited, ";
cout << data[1].count <<" remain for purchase." << endl;
break;

case 3:

if (data[2].count > 0)
data[2].count = vending.dailyReport(change, data[2].count);
cout <<"\n";
cout << data[2].name << " Status:" <<endl;
cout << "$" << data[2].cost++ << " deposited, ";
cout << data[2].count <<" remain for purchase." << endl;
break;

case 4:

if (data[3].count > 0)
data[3].count = vending.dailyReport(change, data[3].count);
cout <<"\n";
cout << data[3].name << " Status:" <<endl;
cout << "$" << data[3].cost++ << " deposited, ";
cout << data[3].count <<" remain for purchase." << endl;

break;

case 5:

if (data[4].count > 0)
data[4].count = vending.dailyReport(change, data[4].count);
cout <<"\n";
cout << data[4].name << " Status:" <<endl;
cout << "$" << data[4].cost++ << " deposited, ";
cout << data[4].count <<" remain for purchase." << endl;
break;
}
cout << "\nIf you would like to make another purchase enter 1 to continue or 0 to stop.\n";
cin >> continuing;
}
return 0;
}
Last edited on
Please edit your code using code blocks, and if you could for the sake of readability put some tabbing in as well (The second is just a minor suggestion).
Sorry, that's how it copy/pasted.
I don't know how to make it look better. :(
this will show you how to use code tags.
http://www.cplusplus.com/articles/z13hAqkS/

if (choice = 5) should be if (choice == 5) this is in your buy drink function
Last edited on
Thank you Yanson!

Topic archived. No new replies allowed.