2014-05-01 58 views
0

這是我修改後的代碼,我得到的文件閱讀,它幾乎工作。我現在遇到的問題是價格只是賣給我的最後價格而不是收取所有的價格。我知道這一定是一個簡單的解決方案,但由於某種原因,我無法弄清楚我需要做些什麼來解決這個問題。修改後的代碼。我不能讓它增加我的銷售價格

#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; 
    } 
+0

你的母語是什麼? – Beta

+0

母語是英語@beta – keggy

+0

您的文字很難讀。你想通過引用傳遞給這個函數什麼?你想讓它返回什麼?如何涉及一個文件? – Beta

回答

0

那麼你需要創建一個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; 
    } 

這是一個非常醜陋的方式做什麼你想這樣做...但是因爲你說你在計算機科學1,我不知道你是否知道對象.....所以如果你不知道他們,你可能想要爲每個對象創建一個數組事情你想從文件中獲得。並使用索引作爲訪問「每個項目」的方式。希望能幫助到你。

0

創建一個包含所有返回值的結構體。含義:

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

然後讓你的函數返回一個引用到結構,這將是這樣的:

Info& CombineAllFunctions() 
{ 
} 
0

我不明白你的問題,但我會給出怎樣的例子使用面向對象設計來執行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; 
    } 
}; 

你可以這樣使用:

Item new_item; 
new_item.Input_From_User(std::cin, std::cout); 

建立在這個基礎上,你可以添加一個我的ThOD將項目寫入文件:

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"; 
    } 
}; 

的用法是:

Item new_item; 
new_item.Output_As_CSV(my_file); 

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

上面的代碼滿足所述要求的功能組,並通過引用將它們傳遞。