Hi, So I am working on a drink machine simulation. I have a text file that holds the prices, names and inventory. My problem is It won't let me set my prices at 1.00 or higher. Ex: I set the price to 1.50 in the text file then when the program reads the info from the text it will only read the cents so instead of 1.50 it will read .50
textfile looks like this:
Apple Juice 1.50 20
Mango Juice 1.50 20
Sprite Mix 1.90 20
Coca Cola 1.90 20
my code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
struct Machine
{
string name;
double cost;
int num;
};
void init(Machine []);
int menu(Machine[]);
void payment(double);
int main()
{
Machine drink[5];
int choice;
double made=0;
init(drink);
choice=menu(drink);
while(choice!=5)
{
payment(drink[choice].cost);
made+=drink[choice].cost;
drink[choice].num--;
choice=menu(drink);
}
cout<<"Today the machine has made $"<<setprecision(2)<<fixed<<made<<endl;
system("pause");
return 0;
}
void payment(double p)
{
double pay;
cout<<"Your drink costs $"<<setprecision(2)<<fixed<<p<<endl;
cout<<"Enter payment: ";
cin>>pay;
while(pay<0||pay>1.||pay<p)
{
cout<<"please insert the correct amount for your drink!\n";
cout<<"maximum payment is $1.00\n";
cout<<"Enter payment: ";
cin>>pay;
}
cout<<"Your change is: $"<<setprecision(2)<<fixed<<pay-p<<endl;
return;
}
void init(Machine d[])
{
ifstream infile("DrinkMachineInventory.txt");
if(infile.fail())
{
cout << "Could not find the file DrinkMachineInventory.txt \n";
cout << "Exiting the program\n";
exit(0);
}
int i=0;
char ch;
string word= "";
while(!infile.eof())
{
word= "";
ch = infile.get();
while(true)
{
if(isdigit(ch) || ch == '\n')
break;
else
word += ch;
ch = infile.get();
}
if(word != "")
{
d[i].name = word;
infile >> d[i].cost >> d[i].num ;
i++;
}
}
infile.close();
}
int menu(Machine d[])
{
int choice=8,i;
bool soldout=true;
while((choice<1||choice>6)||soldout)
{
soldout=false;
cout<<"Menu\n";
cout<<" Drink Cost\tleft\n";
for(i=0;i<5;i++)
{
cout<<i+1<<". "<<setw(15)<<left<<d[i].name<<setw(5);
cout<<setprecision(2)<<fixed<<d[i].cost<<"\t"<<d[i].num<<endl;
}
cout<<"6. Exit\n";
cout<<"Enter Choice ";
cin>>choice;
if(choice<1||choice>6)
cout<<"invalid entry\n";
else
if(d[choice-1].num==0)
{cout<<"sold out\n";
soldout=true;
}
}
return choice-1;
}