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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepu AbrahamR & D Engineering ManagerCommented:
The problem is here in
printf("%s", ID);

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

Best Regards,
DeepuAbrahamK


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.