I don't understand your question, but I'll give an example of how to do the I/O using Object Oriented design.
From your posted code, an Item has a cost and name:
struct Item
{
unsigned int cost;
std::string name;
};
You could add an input method to the structure for obtaining the object's name and cost from the user:
struct Item
{
unsigned int cost;
std::string name;
void Input_From_User(std::istream& input, std::ostream& output)
{
output << "Enter the item name: ";
output.flush();
input >> name;
output << "\nEnter the cost of the item: ";
output.flush();
input >> cost;
}
};
You could use this like:
Item new_item;
new_item.Input_From_User(std::cin, std::cout);
Building upon this foundation, you could add a method to write an item to a file:
struct Item
{
unsigned int cost;
std::string name;
void Input_From_User(std::istream& input, std::ostream& output)
{
output << "Enter the item name: ";
output.flush();
input >> name;
output << "\nEnter the cost of the item: ";
output.flush();
input >> cost;
}
void Output_As_CSV(std::ostream& output)
{
output << cost;
output << ", ";
output << name;
output << "\n";
}
};
The usage would be:
Item new_item;
new_item.Output_As_CSV(my_file);
Edit 1: Structure of function pointers
Free standing functions can be grouped using a structure of function pointers.
typedef std::string (*P_Get_Name_Function)(void);
typedef double (*P_Get_Cost_Function)(void);
typedef unsigned int (*P_Get_Quantity_Function)(void);
struct Item_Functions
{
P_Get_Name_Function name_function;
P_Get_Cost_Function cost_function;
P_Get_Quantity_Function qty_function;
};
You could use it like:
void Input_Items(const Item_Functions& inp_funcs)
{
std::string item_name = (inp_funcs.name_function)();
double item_cost = (inp_funcs.cost_function)();
unsigned int item_quantity = (inp_funcs.qty_function)();
};
Item_Functions User_Item_Funcs =
{ getItemName, getItemCost, getItemQuantity};
// ...
Input_Items(User_Item_Funcs);
The above code satisfies the requirement to group functions and pass them by reference.