不断收到相同的错误...怎么了?C++

Keep getting the same errors... what's wrong? C++

本文关键字:错误 怎么了 C++      更新时间:2023-10-16

请指出具体需要在哪里以及需要进行哪些新的特定编辑我总是犯同样的错误,我不知道出了什么问题。我已经挖了一百万次苦咸水,我很确定我做得对:

  • 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.txtA 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是没有用的,因为字符串有一个默认的构造函数将其创建为空
  • 我建议您在像ifwhile这样的关键字和左括号之间加一个空格
  • 此外,你的间距应该保持一致。虽然totalAmounts后面有空格,但对于其他函数,情况并非如此
  • 也用于变量的命名。为什么fileName使用驼色大小写,而taxrate使用小写。如果你使用小写,那么你应该使用_来分隔单词(例如tax_rate(,因为这样更容易阅读
  • 命名变量时应避免缩写。total_net(或totalNet(比tnet更容易理解
  • 通常,当您遇到编译器错误时,第一个问题就在编译器报告的位置附近。修复该错误,然后检查其他错误是真实错误还是第一个错误的结果。在这种情况下,它有助于编译单个文件(对于具有数百个文件的大型生产项目(