提问人:keggy 提问时间:5/3/2014 最后编辑:BenMorelkeggy 更新时间:7/26/2014 访问量:126
程序在 c++ 中使用带有引用参数的传递值读取文件
Program reads in file using pass by value with reference parameters in c++
问:
这是我正在使用的代码。现在我知道数组可能是更好的选择,但这就是教授想要的方式。当我尝试运行该程序时,它只是无限循环,但似乎我快到了。我是 c++ 的新手,不太了解这门语言,只是基础知识。程序需要首先读取项目、成本和编号。然后稍后读取不同的销售价格,然后输出项目名称、项目成本、销售价格、毛利和净利润。如果有人能为我指出我出错的方向,我将不胜感激。
谢谢
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//all functions needed for this project
void readSellingFile(ifstream &fp, int &number);
double grossprofit(double total, double cost);
double netprofit(double gross, double total);
void getDataFile(ifstream &fp, string &item, int &number, double &cost);
void display(string item,double total, double cost,double gross,double net);
//main function starts here
int main()
-=--:----F1 project5.cpp Top L1 (C++/l Abbrev)---------------------------
For information about GNU Emacs and the GNU system, type C-h C-a.
File Edit Options Buffers Tools C++ Help
fp>>number;
fp>>cost;
}
//the selling cost of the item
void readSellingFile(ifstream &fp, double &selling)
{
double total;
fp>>selling;
selling+=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;
}
答: 暂无答案
上一个:反直觉对象参数行为
评论
selling+=total;
调用未定义的行为。您从未为 赋值,因此它是不确定的。我认为前一行应该是 ,尽管我质疑这个函数中参数和本地变量的名称。目前还不完全清楚。total
fp >> total;
main