C++ 修订守则。我不能把它加在我的售价上

C++ 修订守则。我不能把它加在我的售价上,c++,parameters,reference,C++,Parameters,Reference,这是我修改过的代码,我把文件读了进去,它几乎可以工作了。我现在遇到的问题是销售价格,它只接受我最后的销售价格,而不是收集所有价格。我知道这一定是一个简单的解决办法,但由于某些原因,我只是不知道我需要做什么来解决这个问题 #include<string> #include <iostream> #include <iomanip> #include <fstream> using namespace std; //all functions nee

这是我修改过的代码,我把文件读了进去,它几乎可以工作了。我现在遇到的问题是销售价格,它只接受我最后的销售价格,而不是收集所有价格。我知道这一定是一个简单的解决办法,但由于某些原因,我只是不知道我需要做什么来解决这个问题

#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;
   }
#包括
#包括
#包括
#包括
使用名称空间std;
//该项目所需的所有功能
作废readSellingFile(ifstream和fp、double和selling);
双倍总利润(双倍总额,双倍成本);
净利润加倍(毛额加倍,总额加倍);
双倍总价(双倍销售);
void getDataFile(ifstream&fp、string&item、double&cost、int&number);
作废显示(字符串项、双倍合计、双倍成本、双倍毛额、双倍净额);
//主要功能从这里开始
int main()
{
int i;
双倍总额、净额、销售总额;
ifstream-fp;
字符串项;
整数;
双重成本;
fp.open(“sales.dat”);
如果(!fp)
{

cout创建一个包含所有返回值的结构。意思是:

struct Info
{
    std::string name;
    double cost;
    double numberOfItems;
}
然后让函数返回对该结构的引用,如下所示:

Info& CombineAllFunctions()
{
}

我不理解你的问题,但我将给出一个如何使用面向对象设计进行I/O的示例

根据您发布的代码,项目具有成本和名称:

struct Item
{
  unsigned int cost;
  std::string  name;
};
您可以向结构中添加输入方法,以便从用户处获取对象的名称和成本:

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;
  }
};
基于这个基础,您可以添加一个方法将一个项目写入文件:

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";
  }
};
编辑1:函数指针的结构
独立函数可以使用函数指针的结构进行分组

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;
};
你可以像这样使用它:

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);

上面的代码满足对函数进行分组并通过引用传递它们的要求。

您需要创建一个fstream对象

像这样

  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++;
}
然后可以创建一个for循环来显示

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

    cout<<"Name: " <<name[i]<<endl;
     cout<<"Cost: "<<cost[i]<<endl; 
    cout<<"Quantity: "<<quantity[i]<<endl;
   }
for(int i=0;i您的母语是什么?母语是英语@betay您的文本很难阅读。您希望通过引用将什么传递给one函数?您希望它返回什么?文件是如何涉及的?
  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++;
}
    for ( int i = 0 ; i < SIZE; i ++ )
   {

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