Question

This is my revised code which I got the file to read in and it is almost working. The problem I am running into now is for the selling price it is only taking in my last price for selling instead of collecting all of the prices. I know it must be an easy fix but for some reason I just cant figure out what I need to do to fix this.

#include<string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

//all functions needed for this project
void readSellingFile(ifstream &fp,double &selling);
double grossprofit(double total, double cost);
double netprofit(double gross, double total);
double totalPrice(double &selling);
void getDataFile(ifstream &fp, string &item, double &cost, int &number);
void display(string item,double total, double cost,double gross,double net);

//main function starts here
  int main()
{
  int i;
  double gross,net,selling,total;
  ifstream fp;
  string item;
  int number;
  double cost;

  fp.open ("sales.dat");
  if(!fp)
    {
      cout<<"Error Opening the file"<<endl;
    }

  while(!fp.eof())
    {
      getDataFile(fp,item,cost,number);
      for(int i=0;i<number;i++)
        {
         readSellingFile(fp,selling);
         total=totalPrice(selling);
          gross=grossprofit(total,cost);
          net=netprofit(gross,total);

        }
      display(item,total,cost,gross,net);
      cout<<"Bye!"<<endl;
    }

}

void getDataFile(ifstream &fp, string &item, double &cost, int &number)
{
  cout<<"Reading from the file. "<<endl;
  fp>>item;
  fp>>cost;
  fp>>number;
}

//the selling cost of the item
void readSellingFile(ifstream &fp,double &selling)
{
  fp>>selling;
}

double totalPrice(double &selling)
{
  double total=0;
  total+=selling;
  return total;
}

//calculates the gross profit
double grossprofit(double total,double cost)
{
  double gross;
  gross=total-cost;
  return gross;
}

//calculates the net profit
double netprofit(double gross,double total)
{
  double net;
  net=gross-(.06*total)-(.10*total);
  return net;
}

//prints out the results
void display(string item, double total, double cost ,double gross, double net)
  {
    cout<<"Item:\t\t"<<item<<endl;
    cout<<"cost:\t\t$"<<fixed<<setprecision(2)<<cost<<endl;
    cout<<"Selling price:\t$"<<setprecision(2)<<total<<endl;
    cout<<"Gross Profit: \t$"<<setprecision(2)<<gross<<endl;
    cout<<"Net Profit: \t$"<<setprecision(2)<<net<<endl;
   }
Was it helpful?

Solution

well you need to create a fstream object

like this

  fstream file; 


   file.open ( " name of the file ) ;

    string name[SIZE];  //array that will hold the name of each item
    int quantity[SIZE];// array that will hold the quantity of each item
    double cost[SIZE];// array that will hold the cost of each item
int counter = 0; 
    while ( !file.eof())
{
   file>>name[counter]>>cost[counter]>>quantity[counter];
    counter++;
}

then you can create a for loop to display

    for ( int i = 0 ; i < SIZE; i ++ )
   {

    cout<<"Name: " <<name[i]<<endl;
     cout<<"Cost: "<<cost[i]<<endl; 
    cout<<"Quantity: "<<quantity[i]<<endl;
   }

This is a very ugly way to do what you want to do.. but since you said you are in computer science 1 , i am no sure if you know about objects ..... so if you don't know about them, you may want to create an array for each thing you want from the file. And use the index as a way to access "each item " . Hope it helps.

OTHER TIPS

Make a struct that contains all of your return values. Meaning:

struct Info
{
    std::string name;
    double cost;
    double numberOfItems;
}

Then make your function return a reference to that struct, which will look like:

Info& CombineAllFunctions()
{
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top