LAB: Parsing food data in C++
Write the code in c++
Given a text file containing the availability of food items, write a program that reads the information from the text file and outputs the available food items. The program first reads the name of the text file from the user. The program then reads the text file, stores the information into four separate arrays, and outputs the available food items in the following format: name (category) -- description
Assume the text file contains the category, name, description, and availability of at least one food item, separated by a tab character ('\t').
Hints: Use the find() function to find the index of a tab character in each row of the text file. Use the substr() function to extract texts separated by the tab characters.
Ex: If the input of the program is:
food.txt
and the contents of food.txt are:
Sandwiches Ham sandwich Classic ham sandwich Available
Sandwiches Chicken salad sandwich Chicken salad sandwich Not available
Sandwiches Cheeseburger Classic cheeseburger Not available
Salads Caesar salad Chunks of romaine heart lettuce dressed with lemon juice Available
Salads Asian salad Mixed greens with ginger dressing, sprinkled with sesame Not available
Beverages Water 16oz bottled water Available
Beverages Coca-Cola 16oz Coca-Cola Not available
Mexican food Chicken tacos Grilled chicken breast in freshly made tortillas Not available
Mexican food Beef tacos Ground beef in freshly made tortillas Available
Vegetarian Avocado sandwich Sliced avocado with fruity spread Not available
the output of the program is:
Ham sandwich (Sandwiches) -- Classic ham sandwich
Caesar salad (Salads) -- Chunks of romaine heart lettuce dressed with lemon juice
Water (Beverages) -- 16oz bottled water
Beef tacos (Mexican food) -- Ground beef in freshly made tortillas
Food.txt file
Sandwiches Ham sandwich Classic ham sandwich Available
Sandwiches Chicken salad sandwich Chicken salad sandwich Not available
Sandwiches Cheeseburger Classic cheeseburger Not available
Salads Caesar salad Chunks of romaine heart lettuce dressed with lemon juice Available
Salads Asian salad Mixed greens with ginger dressing, sprinkled with sesame Not available
Beverages Water 16oz bottled water Available
Beverages Coca-Cola 16oz Coca-Cola Not available
Mexican food Chicken tacos Grilled chicken breast in freshly made tortillas Not available
Mexican food Beef tacos Ground beef in freshly made tortillas Available
Vegetarian Avocado sandwich Sliced avocado with fruity spread Not available

Respuesta :

Answer:

Implementation of the given problem in C++:

#include <bits/stdc++.h>

using namespace std;

int main() {

 vector<string> category, name, description, availability;

 string str;

 cout << "Enter the name of text file to open: ";

 cin >> str;

 ifstream file(str);

 while (getline(file, str)) {

   size_t pos1 = str.find('\t');             // to find the first tab delimiter

   size_t pos2 = str.find('\t', pos1 + 1);   // to find the second tab delimiter

   size_t pos3 = str.find('\t', pos2 + 1);   // to find the third tab delimiter

   category.push_back(str.substr(0, pos1));

   name.push_back(str.substr(pos1 + 1, pos2 - pos1 - 1));

   description.push_back(str.substr(pos2 + 1, pos3 - pos2 - 1));

   availability.push_back(str.substr(pos3 + 1, str.length() - pos3));

 }

 for (int i = 0; i < name.size(); i++)

   if (availability[i] == "Available")

     cout << name[i] << " (" << category[i] << ") -- " << description[i] << endl;

}

Output:-

Ver imagen tallinn

The program is an illustration of file manipulations in C++.

File manipulation involves writing to and reading from a file

The program in C++ where comments are used to explain each line is as follows:

#include <bits/stdc++.h>

using namespace std;

int main() {

   //This declares all vector variables

   vector<string> catg, name, desc, status;

   //This declares the file name as a string variable

   string fname;

   //This prompts the user for the filename

   cout <<"Filename: ";

   //This gets input for the filename

   cin >> fname;

   //This opens the file

   ifstream file(fname);

   //The following is repeated till the end of the file

   while (getline(file, fname)) {

       //This finds the first tab delimiter

       size_t pos1 = fname.find('\t');

       //This finds the second tab delimiter

       size_t pos2 = fname.find('\t', pos1 + 1);

       //This finds the third tab delimiter

       size_t pos3 = fname.find('\t', pos2 + 1);

       //The next four lines populate the vector variables

       catg.push_back(fname.substr(0, pos1));

       name.push_back(fname.substr(pos1 + 1, pos2 - pos1 - 1));

       desc.push_back(fname.substr(pos2 + 1, pos3 - pos2 - 1));

       status.push_back(fname.substr(pos3 + 1, fname.length() - pos3));

   }

   //This iterates through the file, line by line

   for (int i = 0; i < name.size(); i++){

       //This prints each line

       if (status[i] == "Available"){

           cout << name[i] << " (" << catg[i] << ") -- " << desc[i] << endl;

       }

   }

   return 0;

}

Read more about similar programs at:

https://brainly.com/question/15456319