Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with writing a program in C for this problem: Please include code to

ID: 3776979 • Letter: P

Question

Please help with writing a program in C for this problem:

Please include code to copy/paste.

Write a program that will simulate a soft drink machine that dispenses four types of soft drinks. The program will accept character input only. The symbols 'c', 'o', 'L', and 'S' will stand for the drinks (Cola, Orange, Lemon, and Spritzer). The symbols 'N', 'D', and 'Q' will stand for the coins (nickel, dime, and quarter). No other coins will be accepted. The character 'R' when read as input will stand for the coins return. Any time that an 'R' is input, all money should be returned to the user. The program will output messages that simulate the output from the soft drink machine. For example, "Cola dispensed," "30 cents returned," "Sorry, out of Spritzer," or "Your change is 10 cents." In addition to accepting input of the money and drink selections, and displaying messages to the user, the program will internally keep track of the amount of change, and the inventory of each drink. A sample run follows. Enter 65 cents for a drink > Q 25 cents received > D 35 cents received > D 45 cents received > Q 70 cents received Make your drink selection now > O Orange drink dispensed 5 cents in change given Enter 65 cents for a drink > Q 25 cents received > Q 50 cents received > C Sorry, insufficient funds entered Please enter additional 15 cents > R 50 cents returned Enter 65 cents for a drink > Write your program in a modular structured style with the different modules in separate source files. Plan the communication between the different parts of the program. Minimize your use of global variables.

Explanation / Answer

#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! ";

        cout<<"maximum payment is $1.00 ";

        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 ";

        cout << "Exiting the program ";

        exit(0);

    }

    int i=0;

    char ch;

    string word= "";

    while(!infile.eof())

    {

        word= "";

        ch = infile.get();

        while(true)

        {

            if(isdigit(ch) || ch == ' ')

                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 ";

        cout<<"      Drink      Cost left ";

        for(i=0;i<5;i++)

        {

            cout<<i+1<<". "<<setw(15)<<left<<d[i].name<<setw(5);

            cout<<setprecision(2)<<fixed<<d[i].cost<<" "<<d[i].num<<endl;

        }

        cout<<"6. Exit ";

        cout<<"Enter Choice ";

        cin>>choice;

        if(choice<1||choice>6)

            cout<<"invalid entry ";

        else

            if(d[choice-1].num==0)

            {cout<<"sold out ";

        soldout=true;

        }

    }

    return choice-1;

}