Calling constructors causes seg fault

I have been at this for about 2 hours.  The rest of my code works great.  The problem is that everytime I call the constructor it keeps seg faulting.  I'm thinking this maybe a simple formatting issue.  Does anything look wrong with this?

Thanks,
JOe K.
Student::Student() : studentID(0), credits(0), GPA(0.0){
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.
		//studentID = 0;
		//credits = 0;
		//GPA = 0.0;
	}
	
    Student::Student(int ID) : studentID(ID), credits(0), GPA(0.0){
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.
		//studentID = ID;
		//credits = 0;
		//GPA = 0.0;
	}
    Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
    // Constructs a student with the given ID, number of credits, and GPA.
		printf("%s", ID);
		//studentID = ID;
		//credits = cr;
		//GPA = grPtAv;
	}

Open in new window

ClaudeWalkerAsked:
Who is Participating?
 
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
The problem is here in
printf("%s", ID);

it should be
printf("%d",ID);

Best Regards,
DeepuAbrahamK


0
 
ClaudeWalkerAuthor Commented:
I still get a seg fault just using this

Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
}
 
//its called from this header
 
#ifndef STUDENT_H
#define STUDENT_H
 
class Student {
 
  private:
 
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
};
 
#endif

Open in new window

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Here is the code...You could try this one...

Best Regards,
DeepuAbrahamK
//Header file----#include "ee.h"
 
//its called from this header
 
#ifndef STUDENT_H
#define STUDENT_H
 
class Student {
 
private:
	int studentID,credits;
	double GPA;
public:
 
	Student(int ID, int cr, double grPtAv);
  
	int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
};
 
#endif
 
//---cpp file --ee.cpp
 
#include "stdafx.h"
#include "ee.h"
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
 
 
Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv)
{
}
 
int  Student::getCredits() const
{
	return credits;
}
 
int Student::getID() const
{
	return studentID;
}
 
double Student::getGPA() const
{
	return GPA;
}
 
 
int main()
{
	Student s(1,2,3.3);
	printf("%d",s.getID());
	printf("%d",s.getCredits());
	printf("%f",s.getGPA());
}

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ClaudeWalkerAuthor Commented:
Thanks for your help so far.  I'll give you a little more information.  I'm working on a Linux machine and I've got a black box object file calling my constructor for Student.  Every time the constructor is called I get a segmentation fault.

The first  part of the code snippet shows the constructors in the .cpp file, while the second shows the entire .h file.  Any ideas why I'm still getting a segmentation fault?
    Student::Student() : studentID(0), credits(0), GPA(0.0){
		
	}
	
    Student::Student(int ID) : studentID(ID), credits(0), GPA(0.0){
		//printf("%d", studentID);
		
	}
    Student::Student(int ID, int cr, double grPtAv) : studentID(ID), credits(cr), GPA(grPtAv){
		
	}
 
===============================================
 
#ifndef STUDENT_H
#define STUDENT_H
 
/*
 * Student class
 *
 * A Student object contains a student ID, the number of credits, and an
 * overall GPA.
 */
class Student {
 
  public:
    
    Student();
    // Constructs a default student with an ID of 0, 0 credits, and 0.0 GPA.
 
    Student(int ID);
    // Constructs a student with the given ID, 0 credits, and 0.0 GPA.
 
    Student(int ID, int cr, double grPtAv);
    // Constructs a student with the given ID, number of credits, and GPA.
 
    // Accessors
    int getID() const;       // returns the student ID
    int getCredits() const;  // returns the number of credits
    double getGPA() const;   // returns the GPA
 
    // Other methods
 
    void update(char grade, int cr);
    // Updates the total credits and overall GPA to take into account the
    // additions of the given letter grade in a course with the given number
    // of credits.  The update is done by first converting the letter grade
    // into a numeric value (A = 4.0, B = 3.0, etc.).  The new GPA is 
    // calculated using the formula:
    //
    //            (oldGPA * old_total_credits) + (numeric_grade * cr)
    //   newGPA = ---------------------------------------------------
    //                        old_total_credits + cr
    //
    // Finally, the total credits is updated (to old_total_credits + cr)
 
    void print() const;  
    // Prints out the student to standard output in the format:
    //   ID, credits, GPA
    // Note: the end-of-line is NOT printed after the student information 
 
  private:
    int studentID;
    int credits;
    double GPA;
};
 
#endif

Open in new window

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
>>I've got a black box object file calling my constructor for Student.
Do you have the code snippet?  Will you be able to recompile it...? It could be giving a fault within the calling function..

0
 
ClaudeWalkerAuthor Commented:
The black box code snippet is assumed to work correctly.  Even if I call the constructor explicitly elsewhere, I get the segmentation fault.  I'm at a loss for what to do.  Thanks again for the help guys.
0
 
ClaudeWalkerAuthor Commented:
P.S. Any other ideas?
0
 
ClaudeWalkerAuthor Commented:
I figured it out...ended up being a string formatting issue.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.