2uzhan.com
Advertisement
Now Place:2uzhan.com » Order/Purchase Design. Input logic.

Order/Purchase Design. Input logic.

Software Development @ August 29, 2009   Views:0

I'm having trouble overloading >> in a fairly specific manner. When the program reads from a file, I want it to read the data into multiple classes. However, you can't pass more than 2 arguments to an istream overload. I've fiddled around with making one class a base class of the other, but I don't think that will work out either. Here is the code.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iterator>

using namespace std;

class Purchase;

class Order {
public:
    string name;
    string address;
    vector<Purchase> items;
};

class Purchase{
public:
    string product_name;
    double unit_price;
    int count;
    Purchase() {}
    Purchase(string pn, double up, int c) :product_name(pn), unit_price(up), count(c) {}
};

istream& operator>>(istream& in, Order& o, Purchase& p)
{
    getline(in, o.name);
    getline(in, o.address);
    getline(in, p.product_name);
    in >> p.unit_price >> p.count;
    o.items.push_back(Purchase(p.product_name, p.unit_price, p.count));
    return in;
}

int main)
{
    string file;
    cout << "Enter file to read orders from: ";

    while (true) {
    cin >> file;
    ifstream is(file.c_str());
    if(!is) cout << "File does not exist.\n";
    else break;
    }

    istream_iterator<Order> ii(is);
    istream_iterator<Order> eos;
    vector<Order> orders(ii,eos);

This shows what I want to do, and the error is obviously in the istream operator overload. I'm not even sure if that overload will work correctly yet, but for now I'm focused on reading data into multiple user defined types. Can anyone explain how to do this? Thanks.

To further clarify, an Order can have multiple purchases. This is another roadblock I'm running into. Somehow I need to extract the information in such a way that when a Purchase is extracted, and the Order data along with it, the program will know to put that purchase data in the vector of purchases in Order if the Order data matches Order data that's already been extracted. (Example: John Doe of 117 Cherry Road (an Order) has 3 Purchases).

--------------Solutions-------------

http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/10.19.OpOverload/html/text.html

On this page, look the Complex class example at the bottom. It explains that you need to declare the prototype of the function as "friend" in the class.

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

Write a java program that asks a person for his height (in meters) and weight (in kilograms) and
outputs one of the messages: underweight, normal, or obese, using the BMI ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...
Tags:
© 2018 2uzhan.com Contact