這是我修改後的代碼,我得到的文件閱讀,它幾乎工作。我現在遇到的問題是價格只是賣給我的最後價格而不是收取所有的價格。我知道這一定是一個簡單的解決方案,但由於某種原因,我無法弄清楚我需要做些什麼來解決這個問題。修改後的代碼。我不能讓它增加我的銷售價格
#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;
}
你的母語是什麼? – Beta
母語是英語@beta – keggy
您的文字很難讀。你想通過引用傳遞給這個函數什麼?你想讓它返回什麼?如何涉及一個文件? – Beta