I have completed my vending machine program but returning change is not working properly. What am I doing wrong. Also when you deposit not enough change it is suppose to start over.
Here is the main, classes, and header files.
//main.cpp
#include <iostream.h>
#include <stdlib.h>
#include "candy.h"
void main()
{
int sel;
Candy c(4,50);
Chip ch(6,60);
Gum g(8,70);
Cookies co(10,80);
double pr;
do
{
// system ("cls");
cout<<endl;
cout<<"*** Welcome to CIS-270's Candy Shop ***"<<endl
<<"To select an item enter"<<endl
<<"1. for Candy"<<endl
<<"2. for Chips"<<endl
<<"3. for Gum"<<endl
<<"4. for Cookies"<<endl
<<"9. to exit"<<endl<<endl;
cin>>sel;
while ((sel !=1) && (sel !=2) && (sel !=3)
&& (sel !=4) && (sel !=9))
{
cout << "You have made an invalid selection."
<< "Please enter an item."
<<endl;
cin>>sel;
}
switch(sel)
{
case 1:
{
if (c.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << c.GetPrice() << "cents"
<<endl;
cin >> pr;
if (pr = c.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
c.SetStock(c.GetStock()-1);
}
else if (pr > c.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy.\n"
<< "Your changes is:" << pr - c.GetPrice()
<< endl;
c.SetStock(c.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 2:
{
if (ch.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << ch.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr = ch.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
ch.SetStock(ch.GetStock()-1);
}
else if (pr > ch.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy.\n"
<< "Your changes is:" << pr - ch.GetPrice()
<< endl;
ch.SetStock(ch.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 3:
{
if (g.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << g.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr = g.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
g.SetStock(g.GetStock()-1);
}
else if (pr > g.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy.\n"
<< "Your changes is:" << pr - g.GetPrice()
<< endl;
g.SetStock(g.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 4:
{
if (co.GetStock() <1)
{
cout << "Out of Stock. Please try other goods"
<< endl;
break;
}
else
{
cout <<"Please deposit:" << co.GetPrice() << "cents"<<endl;
cin >> pr;
if (pr = co.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy."
<< endl;
co.SetStock(co.GetStock()-1);
}
else if (pr > co.GetPrice())
{
cout << "Collect your item at the bottom and Enjoy.\n"
<< "Your changes is:" << pr - co.GetPrice()
<< endl;
co.SetStock(co.GetStock()-1);
}
else
cout << "Please deposit again" << endl;
break;
}
}
case 9:
{
cout<<"Goodbye"<<endl;
break;
}
}
}while(sel !=9);
}
//candy.cpp
#include <iostream.h>
#include "candy.h"
Candy::Candy(int i, double d)
{
price = d;
stock = i;
}
Chip::Chip(int i, double d)
{
price = d;
stock = i;
}
Gum::Gum(int i, double d)
{
price = d;
stock = i;
}
Cookies::Cookies(int i, double d)
{
price = d;
stock = i;
}
void Candy::SetStock(int i)
{
stock = i;
}
int Candy::GetStock()
{
return stock;
}
double Candy::GetPrice()
{
return price;
}
void Chip::SetStock(int i)
{
stock = i;
}
int Chip::GetStock()
{
return stock;
}
double Chip::GetPrice()
{
return price;
}
void Gum::SetStock(int i)
{
stock = i;
}
int Gum::GetStock()
{
return stock;
}
double Gum::GetPrice()
{
return price;
}
void Cookies::SetStock(int i)
{
stock = i;
}
int Cookies::GetStock()
{
return stock;
}
double Cookies::GetPrice()
{
return price;
}
//candy.h
class Candy
{
private:
int stock;
double price;
public:
Candy(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};
class Chip
{
private:
int stock;
double price;
public:
Chip(int,double);
void SetStock(int);
int GetStock();
double GetPrice();
};
class Gum
{
private:
int stock;
double price;
public:
Gum(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};
class Cookies
{
private:
int stock;
double price;
public:
Cookies(int,double);
int GetStock();
void SetStock(int);
double GetPrice();
};