调用后缺少函数值
Missing function value after call
我正在做一个班级的项目。该代码根据从文件中读取的数据计算运费。我遇到的问题是在计算中。从体积函数传递的值保持为零。我觉得我没有通过。此外,不需要使用全局变量——特别是对于计算函数——使用参数是我从教授那里得到的最后一个反馈
代码如下:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
void header(int lab_number, char lab_part)
{
cout << "Kevin Schultzn";
cout << "Lab" << lab_number << lab_part << endl << endl;
}
double length, width, height, x, weight, total = 0, shipping_cost, volume;
int main ()
{
double calculate(double length, double width, double height);
double volume;
double chargeAmount;
double charge(ifstream & inFile, ofstream & prt);
ifstream inFile;
ofstream prt("lab7new_out.txt");
header(7, 'A');
prt << " S & S Global Servicesn";
prt << " Shipping Cost Analysis Reportnn";
prt << "Length Width Height Weight Shippingn";
prt << " Costnn";
inFile.open("c:\lab7\pkg.txt");
if (!inFile)
cout << "Error opening the filen";
inFile >> length;
inFile >> width;
inFile >> height;
inFile >> weight;
volume = calculate(length, width, height);
chargeAmount = charge(inFile, prt);
prt << "------------------------------------" << endl;
prt << "nTotal cost: $" << total;
system("pause");
return 0;
}
double calculate(double length, double width, double height)
{
double volume;
volume = length * width * height;
return volume;
}
double charge(ifstream & inFile, ofstream & prt)
{
const double basic_charge = 12;
const double Vsurcharge = 5;
const double Dsurcharge = 4;
const double Wsurcharge = 2;
double netWeight = 0;
while (!inFile.eof())
{
shipping_cost = basic_charge;
if (volume > 7)
shipping_cost += Vsurcharge;
if (length > 3 || width > 3 || height > 3)
shipping_cost += Dsurcharge;
if (weight > 50)
{
netWeight = weight - 50;
shipping_cost += netWeight * Wsurcharge;
}
total += shipping_cost;
prt << setw(4) << right << setprecision(1) << length << setw(6) << right << setprecision(1) << width << setw(6) << right << setprecision(1) << height << setw(8) << right;
prt << weight << setw(5) << right << setprecision(2) << fixed << "$" << shipping_cost << endl;
inFile >> length;
inFile >> width;
inFile >> height;
inFile >> weight;
}
return total;
}
任何帮助将是伟大的!
正如你的教授所说,请不要使用全局变量。在你的代码中,你有不同的变量volume
,我怀疑你知道你什么时候访问哪个变量。让我们浏览一下您的代码:
double volume; // global variable, never written, read in function charge
int main() {
...
double volume; // local variable, only visible in function main, but never used
...
}
double calculate(double length, double width, double height) {
double volume; // local variable, only visible in calculate
volume = ...;
return volume;
}
double charge(ifstream & inFile, ofstream & prt) {
...
if(volume > 7) // no local variable volume defined, so using global variable volume
...
}
同样,你声明了一个函数calculate,但你从不调用它。
请考虑启用尽可能多的编译器警告。
试试这样做。问题是你需要在调用它之前声明你的函数原型,但是如果你把你的主函数放在。c文件的底部,你不需要声明原型函数,希望它是清晰的
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
void header(int lab_number, char lab_part)
{
cout << "Kevin Schultzn";
cout << "Lab" << lab_number << lab_part << endl << endl;
}
double length, width, height, x, weight, total = 0, shipping_cost, volume;
double calculate(double length, double width, double height)
{
double volume;
volume = length * width * height;
return volume;
}
double charge(ifstream & inFile, ofstream & prt)
{
const double basic_charge = 12;
const double Vsurcharge = 5;
const double Dsurcharge = 4;
const double Wsurcharge = 2;
double netWeight = 0;
while (!inFile.eof())
{
shipping_cost = basic_charge;
if (volume > 7)
shipping_cost += Vsurcharge;
if (length > 3 || width > 3 || height > 3)
shipping_cost += Dsurcharge;
if (weight > 50)
{
netWeight = weight - 50;
shipping_cost += netWeight * Wsurcharge;
}
total += shipping_cost;
prt << setw(4) << right << setprecision(1) << length << setw(6) << right << setprecision(1) << width << setw(6) << right << setprecision(1) << height << setw(8) << right;
prt << weight << setw(5) << right << setprecision(2) << fixed << "$" << shipping_cost << endl;
inFile >> length;
inFile >> width;
inFile >> height;
inFile >> weight;
}
return total;
}
int main ()
{
double volume = calculate(length, width, height);
double chargeAmount;
ifstream inFile;
ofstream prt("lab7new_out.txt");
header(7, 'A');
prt << " S & S Global Servicesn";
prt << " Shipping Cost Analysis Reportnn";
prt << "Length Width Height Weight Shippingn";
prt << " Costnn";
inFile.open("c:\lab7\pkg.txt");
if (!inFile)
cout << "Error opening the filen";
inFile >> length;
inFile >> width;
inFile >> height;
inFile >> weight;
chargeAmount = charge(inFile, prt);
prt << "------------------------------------" << endl;
prt << "nTotal cost: $" << total;
system("pause");
return 0;
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销