C++ 工资计算器C和x2B中的链接列表+;

C++ 工资计算器C和x2B中的链接列表+;,c++,segmentation-fault,C++,Segmentation Fault,我必须为一个班级写一个工资计算器程序,我遇到了一些问题。讲师希望我们使用结构的链接列表对数据对象集合执行操作 #include <iostream> #include <fstream> #include <string.h> #include <stdlib.h> using namespace std; struct Employee { char *fname, *lname; double salary, bonus, deductio

我必须为一个班级写一个工资计算器程序,我遇到了一些问题。讲师希望我们使用结构的链接列表对数据对象集合执行操作

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>

using namespace std;

struct Employee
{
char *fname, *lname;
double salary, bonus, deduction_percent, Deduction, Net_sal;
Employee *next;
};

int main()
{
char line[1024];
char *first, *last, *salchar, *bonuschar, *deductionchar;
double sal, bonus, deduction;
Employee *head = NULL, *tail, *newp, *curr;
tail = NULL;
newp = NULL;
curr = NULL;
cout<<"hi"<<endl;
fstream myfile ("payroll.txt", ios::in) ;

if (myfile.is_open())
{
    //we want our head to point to the first employee, so we
    //must make our first employee before we iterate
    while ( myfile.good() )
    {
        myfile.getline(line,1024);
        //reads the txt file
        first = strtok (line, " ");
        last = strtok (NULL, " ");
        salchar = strtok (NULL, " ");
        bonuschar = strtok (NULL, " ");
        deductionchar = strtok (NULL, " ");

        sal = atof(salchar);
        bonus = atof(bonuschar);
        deduction = atof(deductionchar);

        newp = new Employee;
        newp->fname = first;
        newp->lname = last;
        newp->salary = sal;
        newp->bonus = bonus;
        newp->deduction_percent = deduction;
        newp->next = NULL;

        if(head == NULL)
        {
            head = newp;

            tail = newp;
        }
        else
        {
            tail->next = newp;
            tail = newp;
        }
#包括
#包括
#包括
#包括
使用名称空间std;
结构雇员
{
字符*fname,*lname;
双倍工资、奖金、扣除百分比、扣除额、净额;
雇员*下一位;
};
int main()
{
字符行[1024];
char*first,*last,*salchar,*bonuschar,*DecretionChar;
双倍sal、奖金、扣减;
员工*head=NULL,*tail,*newp,*curr;
tail=NULL;
newp=NULL;
curr=NULL;
coutbonus=奖金;
新建->扣除额\百分比=扣除额;
newp->next=NULL;
if(head==NULL)
{
头=新的;
tail=newp;
}
其他的
{
tail->next=newp;
tail=newp;
}
这就是我遇到问题的地方。当我编译程序时,我遇到了一个分段错误,在代码中加入了一系列cout语句后,我发现问题出在这个循环上。我想知道是否有更好的方法读取我的文件,或者是否有语法错误阻止循环工作

    myfile.close();  
}
for (curr=head; curr != NULL;curr=curr->next)
{
    cout<<curr->fname << " " <<curr->lname <<" "<< curr->salary<<" "<<curr->bonus<<" "<<curr->deduction_percent<<endl;
    curr = curr->next;
}
curr = head;
newp = head;
while (curr != NULL)
{
    curr->Deduction = (curr->salary + curr->bonus) * curr->deduction_percent;
    curr->Net_sal = (curr->salary + curr->bonus) - curr->Deduction;
}

//delete all the dynamic memory in the program
curr = head;
newp = head;
while (curr != NULL)
{
    curr = curr->next;
    delete newp;
    newp = curr;
}

}
myfile.close();
}
for(curr=head;curr!=NULL;curr=curr->next)
{

cout如果您正在寻找更好的读取文件的方法,我强烈建议您使用stringstreams()它在空间之间进行子串。我的代码不在我的计算机附近,所以我不能给你一个很好的例子,但是你可以将stringstreams和getlines链接在一起,一直读到文件的末尾。如果你使用Netbeans/Visual Studio,请查找调试器,设置一些断点并逐步执行,这就是我解决许多处理问题的方法有一个链表/文件流。

有流的老C。注意,在现实世界中没有人应该像这样编写C++(这是指你的导师而不是你,谁应该教你如何使用STL)。
strtok
将指针返回到
line
缓冲区中。当使用新数据引用line时,旧数据将丢失。当line超出范围时,结构中的所有指针都无效。您必须在结构中存储一个副本。我推荐
std::string
@NeilKirk谢谢您的建议!这一更改以及其他一些更改通过代码消除了我的错误。