For your second programming project you are to write a C pro
Solution
Solution ::
DrinkingSoda_Soda_machineInventory.txt-
chocha-chola 0.75 5
Root Beer 0.80 5
Ginger Ale 0.95 5
Orange soda 0.80 5
Grape soda 0.85 5
// program
// header files
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#include <cctype.h>
// Soda_machine structure
struct Soda_machine
{
string Soda_name;
double Soda_cost;
int Soda_number;
};
void init(Soda_machine []);
int menu(Soda_machine[]);
void payment(double);
int main()
{
Soda_machine drink[5];
int User_choice;
double made=0;
init(drink);
User_choice=menu(drink);
while(User_choice!=5)
{
payment(drink[User_choice].Soda_cost);
made+=drink[User_choice].Soda_cost;
drink[User_choice].Soda_number--;
User_choice=menu(drink);
}
cout<<\"Today the Soda_machine has made $\"<<setprecision(2)<<fixed<<made<<endl;
system(\"pause\");
return 0;
}
void payment(double p)
{
double pay;
cout<<\"Your drink Soda_costs $\"<<setprecision(2)<<fixed<<p<<endl;
cout<<\"Enter payment: \";
cin>>pay;
while(pay<0||pay>1.||pay<p)
{
cout<<\"Insert the correct amount for drink!\ \";
cout<<\"maximum payment is $1.00\ \";
cout<<\"Enter the payment: \";
cin>>pay;
}
cout<<\"Change is: $\"<<setprecision(2)<<fixed<<pay-p<<endl;
return;
}
void init(Soda_machine d[])
{
ifstream infile(\"DrinkSoda_machineInventory.txt\");
if(infile.fail())
{
cout << \"Could not find the file DrinkSoda_machineInventory.txt \ \";
cout << \"Exiting from the program\ \";
exit(0);
}
int i=0;
char c;
string word= \" \";
while(!infile.eof())
{
word= \"\ \ \";
c = infile.get();
while(true)
{
if(isdigit(c) || c == \'\ \')
break;
else
word += c;
c = infile.get();
}
if(word != \"\")
{
d[i].Soda_name = word;
infile >> d[i].Soda_cost >> d[i].Soda_number ;
i++;
}
}
infile.close();
}
int menu(Soda_machine d[])
{
int User_choice=8,i;
bool soldout=true;
while((User_choice<1||User_choice>6)||soldout)
{
soldout=false;
cout<<\"Menu\ \";
cout<<\" Drink Soda_cost\\tleft\ \";
for(i=0;i<5;i++)
{
cout<<i+1<<\". \"<<setw(15)<<left<<d[i].Soda_name<<setw(5);
cout<<setprecision(2)<<fixed<<d[i].Soda_cost<<\"\\t\"<<d[i].Soda_number<<endl;
}
cout<<\"6. Exit\ \";
cout<<\"Enter User_choice \";
cin>>User_choice;
if(User_choice<1||User_choice>6)
cout<<\"invalid entry\ \";
else
if(d[User_choice-1].Soda_number==0)
{cout<<\"sold out\ \";
soldout=true;
}
}
return User_choice-1;
}
/// *** Thank You *** ///



