Thread: segmentation core dump - need help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    segmentation core dump - need help

    Hey all, I'm trying to get this grades program to work... but I'm getting a segmentation core dump error. I am using g++ as my compiler.

    #include <iostream>
    #include <string>
    #include <math.h>

    //Grades
    struct Grad{
    double Assgn[8];
    double Quiz[6];
    double Test[2];
    double Particip;
    };

    //Pointer to student names and grades
    struct ListStu{
    string First, Last;
    Grad Grades;
    ListStu *Next;
    };

    void DisplayOptions();

    class Stu{
    public:
    Stu();
    ~Stu();
    void Read();
    void AddAsgn();
    void AddQuiz();
    void AddTest();
    void Part();
    void Display();
    void ShowStudent();
    double Calculate(ListStu *Rov);
    double AsgnAvg(ListStu *Rov);
    double QuizAvg(ListStu *Rov);
    double TestAvg(ListStu *Rov);
    void Show(ListStu *Rov);
    void SortQuiz(ListStu *Rov);
    void SwapQuiz(int a, int b, ListStu *rov);
    void SortNames();
    private:
    ListStu *Front;
    int qcnt, tcnt, acnt, count;
    };

    void main(){
    char input;
    Stu Students;
    Students.Read();
    Students.SortNames();
    DisplayOptions();
    cin>>input;
    while(input!='x')
    {
    switch(input)
    {
    case 'a': Students.AddAsgn();
    break;
    case 'q': Students.AddQuiz();
    break;
    case 't': Students.AddTest();
    break;
    case 'p': Students.Part();
    break;
    case 'd': Students.Display();
    break;
    case 's': Students.ShowStudent();
    break;
    case 'x': break;
    default: cout<<"Invalid input. Retry."<<endl;
    }
    DisplayOptions();
    cin>>input;
    }cout<<"Goodbye."<<endl;
    }

    Stu::Stu(){
    Front=new ListStu;
    Front->Next=0;
    Front->Grades.Quiz[0]=0;
    Front->Grades.Test[0]=0;
    Front->Grades.Assgn[0]=0;
    tcnt=qcnt=acnt=count=0;
    }

    Stu::~Stu(){
    }

    //Takes in Student Names
    void Stu::Read(){
    string f, l;
    ListStu *P, *Tmp;
    Tmp=Front;
    cout<<"Enter in student names (press quit when ready to continue):"<<endl;
    cin>>f;
    while(f!="quit"){
    cin>>l;
    P=new ListStu;
    P->First=f;
    P->Last=l;
    P->Next=0;
    Tmp->Next=P;
    Tmp=P;
    cin>>f;
    count++;
    cout<<P->First<<P->Last<<endl;
    }
    P->Next=0;
    }

    void Stu::AddAsgn(){
    int cnt=0;
    ListStu *rov;
    rov=Front;
    cout<<"Enter in Assignment Grades "<<count<<" students."<<endl<<endl;
    do{
    cnt++;
    cout<<cnt<<endl;
    cin>>rov->Grades.Assgn[acnt];
    rov=rov->Next;
    }
    while(rov->Next!=0);{
    acnt++;}
    }

    void Stu::AddQuiz(){
    ListStu *rov;
    rov=Front;
    cout<<"Enter in Quiz Grades "<<count<< "students."<<endl<<endl;
    for(rov;rov->Next!=0;rov=rov->Next){
    cin>>rov->Grades.Quiz[qcnt];
    }
    qcnt++;
    }

    void Stu::AddTest(){
    ListStu *rov;
    rov=Front;
    cout<<"Enter in Test Grades "<<count<< "students."<<endl<<endl;
    for(rov; rov->Next!=0;rov=rov->Next){
    cin>>rov->Grades.Quiz[tcnt];
    }
    tcnt++;
    }
    void Stu::Part(){
    cout<<"Input Student Name: ";
    string l, f;
    ListStu *fnd, *rov;
    rov=Front;
    bool foun=false;
    cin>>f;
    cin>>l;
    while(rov->Next!=0){
    if(rov->Last==l && rov->First==f){
    fnd=rov;
    foun=true;
    }
    rov=rov->Next;
    }
    if(foun){
    cout<<"Input new participation grade: ";
    cin>>fnd->Grades.Particip;
    }
    else{
    cout<<"Name Not Found."<<endl;
    }
    }

    void Stu::Display(){
    ListStu *rov;
    rov=Front;
    for(rov;rov->Next!=0;rov=rov->Next){
    Show(rov);
    }
    cout<<endl;
    }

    void Stu::ShowStudent(){
    string f, l;
    cout<<"Input Name: ";
    cin>>f;
    ListStu *fnd, *rov;
    rov=Front;
    bool foun=false;
    cin>>l;
    for(rov;rov->Next!=0;rov=fnd->Next){
    if(rov->Last==l && rov->First==f){
    fnd=rov;
    foun=true;
    }
    }
    if(foun){
    Show(fnd);
    }
    else{
    cout<<"Name Not Found."<<endl;
    }
    }

    void Stu::Show(ListStu *Rov){
    ListStu *rov=Rov;
    cout<<rov->Last<<","<<rov->First<<endl;
    cout<<"Assignments: ";
    for(int x=0; x<acnt; x++){
    cout<<rov->Grades.Assgn[x]<<" ";
    }
    cout<<"- Ave: "<<AsgnAvg(Rov)<<endl;
    cout<<"Quizzes: ";
    for(int x=0; x<qcnt; x++){
    cout<<rov->Grades.Quiz[x]<<" ";
    }
    cout<<"- Ave: "<<QuizAvg(Rov)<<endl;
    cout<<"Tests: ";
    for(int x=0; x<tcnt; x++){
    cout<<rov->Grades.Test[x]<<" ";
    }
    cout<<"- Ave: "<<TestAvg(Rov)<<endl;
    cout<<"Class Participation: "<<rov->Grades.Particip<<endl;
    cout<<"Total Average: "<<Calculate(rov)<<endl<<endl;
    }

    double Stu::AsgnAvg(ListStu *Rov){
    double tmp;
    if(acnt!=0){
    for(int x=0; x<acnt; x++){
    tmp+=Rov->Grades.Assgn[x];
    }
    tmp=tmp/acnt;
    return tmp;
    }
    else
    return 100;
    }

    double Stu::QuizAvg(ListStu *Rov){
    double tmp;
    SortQuiz(Rov);
    if(qcnt!=0 && qcnt!=1)
    {
    for(int x=0; x<qcnt-1; x++){
    tmp+=Rov->Grades.Quiz[x];
    }
    tmp=tmp/(qcnt-1);
    return tmp;
    }
    else
    if(qcnt=0)
    return 100;
    else
    return Rov->Grades.Quiz[0];
    }

    double Stu::TestAvg(ListStu *Rov){
    double tmp;
    if(tcnt!=0){
    for(int x=0; x<tcnt; x++){
    tmp+=Rov->Grades.Test[x];
    }
    tmp=tmp/tcnt;
    return tmp;
    }
    else
    return 100;
    }

    double Stu::Calculate(ListStu *Rov){
    double qavg, tavg, aavg, avg;
    qavg=tavg=aavg=avg=0;
    aavg=AsgnAvg(Rov);
    qavg=QuizAvg(Rov);
    tavg=TestAvg(Rov);
    avg=((aavg*0.5)+(qavg*0.1)+(tavg*0.3)+(Rov->Grades.Particip*0.1));
    return avg;
    }

    void Stu::SortQuiz(ListStu *rov){
    for(int x=0;x<qcnt;x++){
    if(rov->Grades.Quiz[x]<rov->Grades.Quiz[qcnt-1])
    SwapQuiz(x, qcnt-1, rov);
    }
    }

    void Stu::SwapQuiz(int A, int B, ListStu *rov){
    double tmp=rov->Grades.Quiz[A];
    rov->Grades.Quiz[A]=rov->Grades.Quiz[B];
    rov->Grades.Quiz[B]=tmp;
    }

    void Stu::SortNames(){
    ListStu *f, *trav1, *trav2, *sm, *bsm, *l;
    f=sm=bsm=l=trav1=trav2=Front;
    bool FNT=true;
    while(f->Next!=0){
    trav1=sm=f->Next;
    trav2=bsm=f;
    while(trav1->Next!=0){
    trav2=trav1;
    trav1=trav1->Next;
    if(trav1->Last<sm->Last){
    sm=trav1;
    bsm=trav2;
    }
    }
    if(sm->Next==0){
    bsm->Next=0;
    sm->Next=f->Next;
    f->Next=sm;
    f=sm;
    }
    else{
    if(sm==f)
    f=sm;
    else{
    bsm->Next=sm->Next;
    sm->Next=f->Next;
    f->Next=sm;
    f=sm;
    }
    }
    }
    if(FNT){
    Front=sm;
    FNT=false;
    }
    }

    void DisplayOptions(){
    cout<<"Select An Option:"<<endl;
    cout<<"a: add assignment grade"<<endl;
    cout<<"q: add quiz grade"<<endl;
    cout<<"t: add test grade"<<endl;
    cout<<"p: change participation grade"<<endl;
    cout<<"d: display class data"<<endl;
    cout<<"s: print student data"<<endl;
    cout<<"x: exit"<<endl;
    }

    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    When writing code it helps to be a bit of sleuth. In cases like this I start commenting out chunks of code until I am able to compile what's left. Usually the problem can then be found in the last section commented out. By commenting out that section, line by line I can usually get it down to which line the problem appears to be. Then I can access a text book or ask a specific question with pertinent code on a bbs without overworking the people I am asking help from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 12:00 PM
  2. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 09:59 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM
  5. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 PM