Question

Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snacks name, code contains the 2 digit code for the item, price holds the items price, and remaining holds the amount of that item left in the vending machine. You will then create an array snackType snacks [MAX_ITEMS] where MAX_ITEMS will be a constant of type int and will hold the value of 20. The vending machine will not necessary hold 20 different items, but the max is 20, and you will need a counter to keep track of the number of different items in the vending machine. In this program to access an items data, we would have snacks [0].name to retrieve the snack name at position 0 and snacks [0].code retrieves the stack code at position 0 and so on You will read the data from a provided input file of arbitrary number of lines, each line of the input will consist of ItemName itemCode amount price and each item will be separated by a space and each line is separated by and end of file character, so you will need to read the data from a file using an end of file controlled while loop, and you need to maintain a counter after each line read to know the amount of items stored in the vending machine. Then your program will output to the user all the contents in the machine, and the user wil have the option to choose an item by entering an item code (you will just read a string), then the snacks [i].remaining of element i will be decremented by 1, then the program will output all the contents again to the screen (any item that has at at least 1 for its remaining value), of course this will also be a menu driven program that will loop until the user enters N to quit or until the vending machine becomes empty (for all i amountLeft[i] -- 0). You will need to write the following functions int readItems (ifstream& infile, snackType snacks[]) - will read all the contents from the file, and it will keep track of the number of lines read and populate the array by updating each elements field, then it will return an integer that represents the number of items stored in the vending machine

c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define MAX_ITEMS 20


struct snackType {
    string name;
    string code;
    double price;
    int remaining;
};

string format(string str){

   
    for (int i = 0; i<str.length(); i++){
       
        if (i == 0){
           str[i] = toupper(str[i]);
        }
        else {
           str[i] = tolower(str[i]);
        }
    }
    return str;
}

int readItems(ifstream &fin, snackType data[]){
   int count = 0;
   string s1,s2;
   int a1;
   double pr;

   while(!fin.eof()){
       fin >> s1 >> s2 >> a1 >> pr;
      
       data[count].name = format(s1);
      
       data[count].code = format(s2);
       data[count].remaining = a1;
       data[count].price = pr;
      

       count++;
   }
   return count;
}

void outputItem(string n, string c, double pr){
    cout << setw(20) << left << n;
    cout << setw(20) << left << c;
    cout << setw(20) << fixed << setprecision(2) << pr << endl;
}

void bubblesort(int n, snackType data[]){
   string s1,s2;
   int a1;
   double pr;
   for (int i = 0; i<n-1; i++){
       for (int j = 0; j<n-1; j++){
           if (data[i].remaining > data[i+1].remaining){
               s1 = data[i].name;
               s2 = data[i].code;
               a1 = data[i].remaining;
               pr = data[i].price;
               data[i].name = data[i+1].name;
               data[i].code = data[i+1].code;
               data[i].remaining = data[i+1].remaining;
               data[i].price = data[i+1].price;
               data[i+1].name = s1;
               data[i+1].code = s2;
               data[i+1].remaining = a1;
               data[i+1].price = pr;
           }
       }
   }
}

int main(){

   snackType snacks[MAX_ITEMS];

   cout << "Enter file name :";
   string name;
   cin >> name;
   ifstream fin(name.c_str());
   if (!fin){
       cout << "Error opening file\n";
       return 0;
   }
   int n = readItems(fin,snacks);
  
   while(true){
      cout << setw(20) << left << "Item Name";
      cout << setw(20) << left << "Item Code";
      cout << setw(20) << "Price" << endl;   

      bubblesort(n,snacks);
      for (int i = 0; i<n; i++){
         if (snacks[i].remaining > 0)
            outputItem(snacks[i].name, snacks[i].code, snacks[i].price);
      }
      cout << "Enter Item Code:";
      string code;
      cin >> code;
      code = format(code);
      int found = 0;
     
      for (int i = 0; i<n; i++){
          if (code == snacks[i].code){
           
             if (snacks[i].remaining > 0){
                found = 1;
                snacks[i].remaining--;
               
             }
            
          }
      }
      if (found == 1)
         cout << "Good Purchase\n";
      else
         cout << "item not found\n";
      cout << "Continue(Y/N):";
      string ch;
      cin >> ch;
      if (ch[0] == 'N')
         break;
   }
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
c++ question, i put the txt attachment picture for this question... please include comments on calculations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1)...

    Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1) (1) Create three files to submit: • Item ToPurchase.h - Struct definition and related function declarations • Item ToPurchase.c-Related function definitions • main.c-main function Build the ItemToPurchase struct with the following specifications: • Data members (3 pts) • char itemName • int itemPrice • int itemQuantity • Related functions • MakeltemBlank0 (2 pts) Has a pointer to an item To Purchase parameter. Sets item's...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...

    GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) {    // Variable declarations    int fd;                       // file descripter    char buffer[BUF_SIZE];       // string for...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • *Urgent* I've tried getting help with this a couple times, but nobody has answered my questions,...

    *Urgent* I've tried getting help with this a couple times, but nobody has answered my questions, and it's been several days. #################################################################################### This needs to be Python that works in a Zybooks programming window. 1. Build an ItemToPurchase class with the following specifications: Attributes (3 pts) item_name (string) item_price (float) item_quantity (int) Default constructor (1 pt) Initializes item's name = "none", item's price = 0, item's quantity = 0 Method print_item_cost() Example of print_item_cost() output: Bottled Water 10 @ $1...

  • Need help with a C++ problem I have

    For my assignment I have to write a program that creates a restaurant billing system. I think that I got it correct but it says that it is incorrect, so I was wondering if someone would be able to look over my code. It says that I need tax of $0.20, and the amount due to be $4.10, which is what I have in my output.https://imgur.com/a/jgglmvWMy issue is that I have the correct outcome when I run my program but...

  • JAVA Write an application to test the HuffmanTree class. Your application will need to read a...

    JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT