Why am I getting a Segmentation fault: 11 in my C++ program?

I am currently experiencing trouble with an Segmentation fault : 11 in my C++ Program which uses object-oriented concepts:

//
//  main.cpp
//  pointersInOOP
//
//  Created by Jayant Raul Rao on 15/05/15.
//  Copyright (c) 2015 Jayant Raul Rao. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstdlib> 

using namespace std;

class Student
{
public:

    char * name;
    int semesterHours;
    float gpa;
};



int main(int argc, const char * argv[]) {

    cout<<"Beta Version of 1.0 Student constructor. n";
    cout<<"n";
    cout<<"n";

    //Declaring a student object and a pointer
    Student s;
    Student *pOnS = &s;

    //take userInput
    printf("Please name the student... : ");
    cin>>pOnS->name;

    printf("Okay, you called your Student %s. Please now input the gpa of the Student:  ", pOnS->name);
    cin>>pOnS->name;
    printf("The gpa of %s is %f", pOnS->name, pOnS->gpa);

    return 0;
}

And when I execute this code I actually give my input and that always ends in a Segmentation fault. This error I get if I execute in the Terminal. If I execute directly in Xcode I get directed to a file called istream where there is written at a line: Bad access 0x1. Therefore I am unable to execute my program. I have already found out what Segmentation fault means.
Something like literally Stack Overflow. But I am unable to really understand any bugfix.

I thank you in advance,

Jayant Raul Rao


Source: c++

Leave a Reply