0

So I have a .txt file looking like this:
1:Meat Dish:Steak:11.5
2:Fish Dish:Fish and chips:12
The first number is the itemNo, 'Meat Dish' is my category, 'Steak' is my description and finally '11.5' is my price.

So basically I want to search for the itemNo and I want it to display the price from that line. This is what I have until now:

#include <iostream>
#include <fstream>
#include <string>
#include <vector> // We will use this to store Players
using std::string;
using std::ofstream; 
using std::ifstream; 
using std::cout;
using std::cin;

struct MenuList // Define a "Player" data structure
{
    string itemNo;
    string category;
    string descript;
    double price;
};

std::istream& operator>>(std::istream& infile, MenuList& menu)
{
    // In this function we will define how information is inputted into the player struct
    // std::cin is derived from the istream class
    getline(infile, menu.itemNo, ':'); 
    getline(infile, menu.category, ':');
    getline(infile, menu.descript, ':');
    infile >> menu.price;
    // When we have extracted all of our information, return the stream
    return infile;
}

std::ostream& operator<<(std::ostream& os, MenuList& menu)
{
    // Just like the istream, we define how a player struct is displayed when using std::cout
    os << "" << menu.itemNo << " " << menu.category << " - " << menu.descript;
    // When we have extracted all of our information, return the stream
    return os;
}

void Load(std::vector<MenuList>& r, string filename) 
{
    std::ifstream ifs(filename.c_str()); // Open the file name
    if(ifs)
    {
        while(ifs.good()) // While contents are left to be extracted
        {
            MenuList temp;
            ifs >> temp;        // Extract record into a temp object
            r.push_back(temp);  // Copy it to the record database
        }
        cout << "Read " << r.size() << " records.\n\n"; 
    }
    else
    {
        cout << "Could not open file.\n\n";
    }
}

void Read(std::vector<MenuList>& r) // Read record contents
{
    for(unsigned int i = 0; i < r.size(); i++)
        cout << r[i] << "\n";
}

void Search(std::vector<MenuList>& r) // Search records for name
{
    string n;
    cout << "Search for: ";
    cin >> n;
    for(int i = 0; i < r.size(); i++)
    {
        if(r[i].itemNo.find(n) != string::npos)
            cout << r[i];
    }
}

int main()
{
    std::vector<MenuList> records;
    Load(records, "delete.txt");
    Read(records);
    Search(records);
    return 0;
}

I don't really know how to make it so it shows just the price without showing the whole line.

Niku Nik
  • 49
  • 1
  • 6
  • `cout << r[i];`->`cout << r[i].price;` – user4581301 May 09 '18 at 23:08
  • Wait it was that simple?!?!?! You are a god. – Niku Nik May 09 '18 at 23:10
  • 1
    Keep an eye on `while(ifs.good())` It's testing that the stream is good before reading the stream and leaving you at the mercy of the read failing undetected before the bad values are used. Similar to the problem described here: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 May 09 '18 at 23:13
  • Going to read that. Thanks a lot again. – Niku Nik May 09 '18 at 23:30
  • You can always look into using `#include `, this library allows for full line strings to be be broken apart, I suggest reading some documentation on http://www.cplusplus.com/reference/sstream/stringstream/. By using the “>>” operator (shown in the documentation), It will allow you to extract only the price from your string. – JakeWebbDev May 09 '18 at 23:34

1 Answers1

0

I have written my own code that reads in your text file and stores the information in a struct. Once you have a vector of MenuList, it is dead simple to only print what you want.

include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct MenuList // Define a "Player" data structure
{
    string itemNo;
    string category;
    string descript;
    double price;
};

void addToList(vector<MenuList*>& vector_menu_list, string& itemNo, string& category, string& descript, string& price)
{
    MenuList* p_menu_list = new MenuList;
    p_menu_list->itemNo = itemNo;
    p_menu_list->category = category;
    p_menu_list->descript = descript;
    p_menu_list->price = std::stod(price);
    vector_menu_list.push_back(p_menu_list);
}

vector<MenuList*> readFile(string filename, bool& error_encountered)
{
    vector<MenuList*> vector_menu_list;

    ifstream file;
    file.open(filename);

    if (file.is_open())
    {
        string itemNo;
        string category;
        string descript;
        string price;
        short number_of_colons_encountered = 0;

        char c;
        while (file.get(c))
        {
            if ('\n' != c && EOF != c)
            {
                if (':' == c)
                {
                    number_of_colons_encountered++;
                    continue;
                }

                switch (number_of_colons_encountered)
                {
                case 0:
                    itemNo += c;
                    break;
                case 1:
                    category += c;
                    break;
                case 2:
                    descript += c;
                    break;
                case 3: 
                    price += c;
                    break;
                default:
                    error_encountered = true;
                    file.close();
                    return vector_menu_list;
                }
            }
            else
            {
                addToList(vector_menu_list, itemNo, category, descript, price);
                itemNo.clear();
                category.clear();
                descript.clear();
                price.clear();
                number_of_colons_encountered = 0;
            }
        }

        addToList(vector_menu_list, itemNo, category, descript, price);

        error_encountered = false;
    }
    else
    {
        error_encountered = true;
    }

    file.close();

    return vector_menu_list;
}
int main()
{
    bool error_encountered;
    vector<MenuList*> p_menu_list = readFile("menu list.txt", error_encountered);

    if (true == error_encountered)
    {
        return -1;
    }

    cout << "List menu items:" << endl;
    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        cout << p_menu_list.at(i)->itemNo << " ";
        cout << p_menu_list.at(i)->category << " ";
        cout << p_menu_list.at(i)->descript << " ";
        cout << p_menu_list.at(i)->price << endl;
    }

    cout << "\n\nHere are only the prices: " << endl;
    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        cout << p_menu_list.at(i)->price << endl;
    }

    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        delete p_menu_list.at(i);
    }
    return 0;
}
Willi
  • 361
  • 6
  • 18