程序在 c++ 中使用带有引用参数的传递值读取文件

Program reads in file using pass by value with reference parameters in c++

提问人:keggy 提问时间:5/3/2014 最后编辑:BenMorelkeggy 更新时间:7/26/2014 访问量:126

问:

这是我正在使用的代码。现在我知道数组可能是更好的选择,但这就是教授想要的方式。当我尝试运行该程序时,它只是无限循环,但似乎我快到了。我是 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;
   }
C++ 参数 按引用传递

评论

1赞 WhozCraig 5/3/2014
selling+=total;调用未定义的行为。您从未为 赋值,因此它是不确定的。我认为前一行应该是 ,尽管我质疑这个函数中参数和本地变量的名称。目前还不完全清楚。totalfp >> total;
0赞 keggy 5/3/2014
@WhozCraig是的,对不起,我是这个新手,阅读文件让我感到困惑,所以我知道很多都是令人困惑的。我有一个程序,用户可以手动执行,但现在我们需要它来读取文件,它真的通过我关闭了。
0赞 Martin James 5/3/2014
停止编写程序,直到您知道如何使用调试器。如果不能使用调试器,则无法开发软件。
0赞 keggy 5/3/2014
@MartinJames 好吧,因为我正在上课,而且这是一个项目,目前还不是一个选择。谢谢
0赞 Edward 5/3/2014
您似乎有一些 emacs 行抹杀了您的部分功能。main

答: 暂无答案