我正在使用orwell dev | C ++
C ++是一种新语言,我来自c#,就像我说70%相同。
这是我的代码
#include<iostream>
#include<fstream>
using namespace std;
///loading libraries
float const taxnet = .9;
float const taxwh = .1;
int employeenumber;
float payrate, gross, net, manhours, overtime, taxes;
///declaring variablies and costants
char more;
char more2;
///user controls
///payroll calculations function
void payrollcalc () {
if (manhours>40) {
overtime= manhours-40;
gross= ((manhours - overtime) * payrate)+((payrate * 1.5)* overtime);
//overtime claculation
}
else {
gross =manhours * payrate;
//no overtime calculation
}
taxes= gross * taxwh;
net = gross * taxnet;
//taxesand net pay calculation
cout<< " Your ID is " << employeenumber <<endl;
cout<< " # of hours worked " << manhours << endl;
cout<< " Your Hourly rate is " << payrate << endl;
cout<< " Your Gross pay is " << gross << endl;
cout<< " Your tax rate is " << taxwh << endl;
cout<< " Amount of taxes " << taxes << endl;
cout<< " Your net pay is " << net << endl;
///writing to file
std::string empnum = std::to_string(employeenumber);
ofstream payroll;
payroll.open (empnum+".txt");
payroll<< " Your ID is " << employeenumber <<endl;
payroll<< " # of hours worked " << manhours << endl;
payroll<< " Your Hourly rate is " << payrate << endl;
payroll<< " Your Gross pay is " << gross << endl;
payroll<< " Your tax rate is " << taxwh << endl;
payroll<< " Amount of taxes " << taxes << endl;
payroll<< " Your net pay is " << net << endl;
payroll.close();
}
main(){
while (more != 27){
//main
cout<< "Hit 1 to enter data hit 2 to recall dat hit esc to exit";
///instructions
newdata:
///call back see line 115
if (more == 49) {
cout<< "Enter Employee ID:";
cin>> employeenumber;
cout<<"Enter Number of Hours Worked:";
cin>> manhours;
cout<<"Enter Pay rate:";
cin>> payrate;
cin>> payrollcalc;
}
else (more == 50) {
olddata:
///call back see line 111
errorreset:
cout<< "Enter employee number";
cin>> employeenumber;
///reading in data
ifstream payroll = employeenumber;
payroll.open(employeenumber".txt");
if (!payroll){
cout>> "Check employeenumber and try agian" endl;
goto errorreset:
///error check
}
cout>> payroll.eof endl;
cout>> endl;
cout>> endl;
cout>> "Press Enter to see another employee number; Press space to enter new employee information; press escape to exit the program" endl;
if (more2 == 13 ){
goto olddata;
}
else (more2 == 32){
goto newdata;
}
///sending back to the loop
}
//entering data
return 0;
}
}
我认为我的问题出在这一部分
std::string empnum = std::to_string(employeenumber);
ofstream payroll;
payroll.open (empnum+".txt");
payroll<< " Your ID is " << employeenumber <<endl;
payroll<< " # of hours worked " << manhours << endl;
payroll<< " Your Hourly rate is " << payrate << endl;
payroll<< " Your Gross pay is " << gross << endl;
payroll<< " Your tax rate is " << taxwh << endl;
payroll<< " Amount of taxes " << taxes << endl;
payroll<< " Your net pay is " << net << endl;
payroll.close();
如果有人可以指引我走出轨道,我会感激不尽,因为我没有想法。
答案 0 :(得分:2)
首先考虑调高编译器的错误/ waring级别,对于gcc / clang,合理的级别为-Wall -Wextra
一个开始。
让我解决一下我在代码中看到的一些问题。
main(){
我们已经遇到了第一个问题。 C ++中主要功能允许的唯一2个签名是int main()
或int main(int argc, char *argv[])
。您可能会因遗留原因而接受(如果您未指定任何内容,则在C中隐式返回类型为int
)但不应使用。
cout>> payroll.eof endl; // this line absolutely makes no sens and you forgot some `<<` in here probably.
cout>> endl;
cout>> endl;
cout>> "Press Enter to see another employee number; Press space to enter new employee information; press escape to exit the program" endl;
'箭头'指向错误的方向。它应该是cout << endl
。要记住它,请将它们视为表示数据流的箭头。 cin >> variable
数据从cin
读取并放入变量cout << variable
,变量将输出到cout
。
然后你得到了一个没有意义的阅读:
cin>> payrollcalc;
payrollcalc是一个函数,我不知道你想在这里做什么,但是你应该把它称为payrollcalc();
,试图从cin
读取它是没有意义的。
您还使用std::string
而没有#include <string>
。另请注意,您应该像我一样在include行中放置一个空格。我不知道这是一个没有顶部空间的问题,但它的空间肯定更具可读性。
现在我应该指出一些不良做法和其他内容。你的缩进和格式非常混乱,很难发现任何你应该看到的清理它的东西。
至于goto
,它被视为不良做法/有害。这是由Dijkstra开始的。现在说它仍然在某些地方使用它并且经常被夸大但是对于你正在使用的简单代码我不认为这是必要的并且可能以更好的结构化方式完成,使逻辑更容易理解记住代码的读取频率远远超过你写的代码,可读性非常重要。如果您对这些问题感到好奇,请@itsnotmyrealname在您的问题上发布了一个链接,供您查看。
此外,您也会遇到不一致的问题,因此难以阅读/混淆。
gross =manhours * payrate; // inconsistent assignments
taxes= gross * taxwh;
net = gross * taxnet;
std::to_string(employeenumber) // inconsistent function calls.
payroll.open (empnum+".txt");
至于全局变量,如果你已经有一些编程经验(就像你说的那样在C#中)那么你可能知道它们也被认为是不好的做法而且应该避免,你应该考虑将它们作为局部变量和将它们作为函数参数传递/从函数返回它们。