不断收到相同的错误...怎么了?C++
Keep getting the same errors... what's wrong? C++
请指出具体需要在哪里以及需要进行哪些新的特定编辑我总是犯同样的错误,我不知道出了什么问题。我已经挖了一百万次苦咸水,我很确定我做得对:
- cpp:36:错误:在"{"标记之前,此处不允许函数定义
- cpp:44:错误:在"{"标记之前,此处不允许函数定义
- cpp:58:错误:"double"之前应为初始值设定项
- cpp:63:错误:在"{"标记之前,此处不允许函数定义
- cpp:69:错误:在"{"标记之前,此处不允许函数定义
代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string item = "";
ifstream fin;
double tgross = 0;
double tnet = 0;
double hourly;
double hours;
double taxrate;
double net;
string fileName = "payroll.txt";
fin.open("payroll.txt");
if(!fin.is_open())
{
void instructions()
{
cout << "This payroll program calculates an individual employee pay and";
cout << "ncompany totals using data from a data file payroll.txt.n";
cout << "nnA payroll report showing payroll information ";
cout << " is displayed.nn";
}
void reportTitle()
{
cout << setprecision(2) << fixed << showpoint << left
<< setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"
<< setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
cout << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked"
<< setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}
}
while(!fin.eof())
{
getline(fin,item,'#');
fin >> hourly >> hours >> taxrate;
double calculateGross(double hours, double hourly)
double calculateNet(double grosspay, double netpercent)
{
return grosspay - grosspay*netpercent/100.0;
}
void displayEmployeeInfo(const string &, double, double, double, double, double)
{
tgross += grosspay;
tnet += net;
}
}
void totalAmounts (double tgross, double tnet)
{
cout << "Totals" << setprecision(2) << fixed << showpoint << right
<< setw(50) << tgross << setw(10) << tnet << endl;
}
fin.close();
}
这段代码给出的警告较少,但您确实必须自己整理代码。我也不想说,全局声明所有变量是一个很好的解决方案。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
string item = "";
ifstream fin;
double tgross = 0;
double tnet = 0;
double hourly;
double hours;
double taxrate;
double net;
void instructions()
{
cout << "This payroll program calculates an individual employee pay and";
cout << "ncompany totals using data from a data file payroll.txt.n";
cout << "nnA payroll report showing payroll information ";
cout << " is displayed.nn";
}
void reportTitle()
{
cout << setprecision(2) << fixed << showpoint << left
<< setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"
<< setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
cout << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked"
<< setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}
double calculateNet(double grosspay, double netpercent)
{
return grosspay - grosspay*netpercent/100.0;
}
void displayEmployeeInfo(const string &, double, double, double, double, double)
{
tgross += grosspay;
tnet += net;
}
void totalAmounts (double tgross, double tnet)
{
cout << "Totals" << setprecision(2) << fixed << showpoint << right
<< setw(50) << tgross << setw(10) << tnet << endl;
}
int main()
{
string fileName = "payroll.txt";
fin.open("payroll.txt");
if(!fin.is_open())
{
instructions();
reportTitle();
}
while(!fin.eof())
{
getline(fin,item,'#');
fin >> hourly >> hours >> taxrate;
double calculateGross(double hours, double hourly);
}
fin.close();
}
您必须将您的功能放在主之前
void instructions()
{
cout << "This payroll program calculates an individual employee pay and";
cout << "ncompany totals using data from a data file payroll.txt.n";
cout << "nnA payroll report showing payroll information ";
cout << " is displayed.nn";
}
顺便说一句,为了保持一致性和提高可读性,您应该将所有行更改都放在行的开头或末尾。否则,就会更难看到,例如,在payroll.txt
和A payroll report
之间有3条线。。。
// Other functions here…
// If some functions are dependant on others, those need to be declared before they are used.
int main()
{
// Some code here…
// Call your function
instructions();
// More code afterwards…
return 0;
}
或者,您只能在main之前声明您的函数,如下所示:
void instructions();
void reportTitle();
double calculateGross(double hours, double hourly);
double calculateNet(double grosspay, double netpercent);
// For documentation purpose, you should name your arguments.
// Also the body of your function does not appears to do what its name suggest.
void displayEmployeeInfo(const string &, double, double, double, double, double);
// Show probably named displayTotalAmounts
void totalAmounts(double tgross, double tnet);
您需要注意,在调用函数时必须传递适当的参数。例如:
int main() // partial implementation
{
double tgross = 1.0; // Whatever code you need to have desired value...
double tnet = 0.90;
totalAmounts(tgross, tnet);
return 0;
}
如果您使用稍后的选项,那么您可以在此处(在main之后(定义其他函数。
这给出了如何构建程序的基本概念。
阅读所有其他注释以查找代码中的其他问题
这里有一些额外的东西:
- 定义变量
fileName
,甚至初始化它,但之后使用字符串打开文件 - 如果需要修改传递给函数的参数中的变量,以便调用方看到更改,则需要通过引用传递。例如:
double &tnet
- 通常,最好在生产代码中避免使用
using namespace std
- 最好在第一次使用变量时声明变量
- 某些变量行
net
似乎从未初始化 - 正如所写的,那么指令
if(!fin.is_open())
似乎是可疑的。假设没有错误,文件将在此时打开,但在这种情况下,您可能希望显示标题 - 用
= ""
初始化string
是没有用的,因为字符串有一个默认的构造函数将其创建为空 - 我建议您在像
if
或while
这样的关键字和左括号之间加一个空格 - 此外,你的间距应该保持一致。虽然
totalAmounts
后面有空格,但对于其他函数,情况并非如此 - 也用于变量的命名。为什么
fileName
使用驼色大小写,而taxrate
使用小写。如果你使用小写,那么你应该使用_来分隔单词(例如tax_rate
(,因为这样更容易阅读 - 命名变量时应避免缩写。
total_net
(或totalNet
(比tnet
更容易理解 - 通常,当您遇到编译器错误时,第一个问题就在编译器报告的位置附近。修复该错误,然后检查其他错误是真实错误还是第一个错误的结果。在这种情况下,它有助于编译单个文件(对于具有数百个文件的大型生产项目(
相关文章:
- 不断收到相同的错误...怎么了?C++
- 编译器没有显示错误,但我没有得到任何输出.怎么了?
- qsort 不适用于我的 impl 中的浮点类型。怎么了?
- 在C++中表现不佳.怎么了?
- 我的程序怎么了?(C )
- 这里怎么了?单位转换器
- 错误:未定义对 .. 的引用.怎么了
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- 我的代码怎么了?带有QT的原始输入API
- 编译时错误:删除了联合默认构造函数
- 程序为某些条目提供了错误的输出!怎么了?
- 我在以下括号验证程序中得到错误的输出?怎么了?
- 错误:请求非类类型的 int* 的成员'insert'?怎么了?
- 尝试对结构向量进行排序时,我在算法文件中收到 20 个错误。我不知道怎么了?
- 编译器错误"character constant too long for its type" 。怎么了?
- 打印链表错误。怎么了?
- 我没有任何编译错误,但我的代码不起作用。怎么了?
- 我的主函数中有一些 "expected primary-expression before 'int' " 类型的错误。怎么了?
- 怎么了?-组合编译错误(与家庭+人类的例子)