For your second programming project you are to write a C pro

For your second programming project, you are to write a C program that will simulate a soda machine! The program is to use a struct that stores the following data members: Soda name Soda cost Number of sodas in machine The original contents of the soda machine are contained in the binary file, \"sodas.dat\", which I have made available with this assignment. It is a file of five structs, each with the members indicated above. Your program is to read this file and place its contents into the array, soda, of data type SodaType. You should keep a safe copy of \"sodas dat\" in a separate folder in case that you nccd to usc it again in your project folder should you nced to fix your program and run your program correctly in the final tcst. SodaType soda where SodaType is the struct type, struct SodaType char name[12]; float price; int num; Soda Type must be declared globally. Your program will then process the array, soda, according to the choices made by the user. The original binary file for the soda machine starts with five sodas each. So when the program runs the first time, a description to the user is to be displayed at the screen and then the following menu of selections is to be displayed at the screen Choose from the following Menu: 1) Cola 0.75 2) Root Beer 0.80 3) Ginger Ale 0.95 4) Orange Soda 0.80 5) Grape Soda 0.85 6) Leave the soda machinc Choose one:

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 *** ///

 For your second programming project, you are to write a C program that will simulate a soda machine! The program is to use a struct that stores the following d
 For your second programming project, you are to write a C program that will simulate a soda machine! The program is to use a struct that stores the following d
 For your second programming project, you are to write a C program that will simulate a soda machine! The program is to use a struct that stores the following d
 For your second programming project, you are to write a C program that will simulate a soda machine! The program is to use a struct that stores the following d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site