title: Segmentation fault when attempting to pass variables to constructor C++
body: <p>I've been attempting to try and switch over to C++ because I switched universities and am trying to experiment with C++ before my courses start. I run into trouble with the differences with classes in java and c++. I keep getting segmentation faults when I try to call the constructor using input variables as arguments.</p>
  <pre><code>    static Course createCourse() //attempt to make a static method that fails as well
  { 
      std::string courseCode;
      std::string courseTitle;
      double creditHrs;
      int numOfStudents;
      std::cout&lt;&lt; &quot;Please enter the course code: \n&quot;;
      std::getline(std::cin, courseCode);
      std::cout&lt;&lt;&quot;Please enter the course title: \n&quot;;
      std::getline(std::cin, courseTitle);
      std::cout &lt;&lt; &quot;Please enter the number of credit hours: \n&quot;;
      std::cin &gt;&gt; creditHrs;
      std::cout &lt;&lt; &quot;Please enter the number of students in the course: \n&quot;;
      std::cin &gt;&gt; numOfStudents;
      Course aye(courseCode, courseTitle, creditHrs, numOfStudents); //seg fault
      return aye;
   }
  
  int main()
  {
      std::string courseCode;
      std::string courseTitle;
      double creditHrs;
      int numOfStudents;
      std::cout&lt;&lt; &quot;Please enter the course code: \n&quot;;
      std::getline(std::cin, courseCode);
      std::cout&lt;&lt;&quot;Please enter the course title: \n&quot;;
      std::getline(std::cin, courseTitle);
      std::cout &lt;&lt; &quot;Please enter the number of credit hours: \n&quot;;
      std::cin &gt;&gt; creditHrs;
      std::cout &lt;&lt; &quot;Please enter the number of students in the course: \n&quot;;
      std::cin &gt;&gt; numOfStudents;
      std::cout &lt;&lt; courseCode &lt;&lt; &quot; &quot; &lt;&lt; courseTitle &lt;&lt; &quot; &quot; &lt;&lt; creditHrs &lt;&lt; &quot; &quot; &lt;&lt; numOfStudents; //just for testing
      Course *newCourse = new Course(courseCode, courseTitle, creditHrs, numOfStudents); //where segmentation fault occurs
      double max = newCourse-&gt;getMax();
      double average = newCourse-&gt;getAverage();
      double min = newCourse-&gt;getMin();
      cout &lt;&lt; &quot;The average of the scores is &quot; &lt;&lt; average &lt;&lt; &quot;\n&quot;;
      cout &lt;&lt; &quot;The max score is &quot; &lt;&lt; max &lt;&lt; &quot;\n&quot;;
      cout &lt;&lt; &quot;The minimum score is &quot; &lt;&lt; min &lt;&lt; &quot;\n&quot;;
  }
  </code></pre>
  <p>Here is my code for the Course class. I really don't know where this error is occurring. All the course methods function as expected when I directly pass the arguments to the constructor.</p>
  <pre><code>{
  private:
    string courseCode;
    string courseTitle;
    double creditHours;
    vector&lt;double&gt; studentScores;
    int numOfStudents;
    double getSum();
  
  public:
    Course();
    Course(string newCourseCode, string newCourseTitle, double newCreditHrs, int numOfStudents);
    void displayScores();
    void readScores();
    double getAverage();
    double getMax();
    double getMin();
  };
  
  //some of my function definitions on separate file
    Course::Course()
    {
        this-&gt;courseCode = &quot;nothing&quot;;
        this-&gt;courseTitle = &quot;empty&quot;;
        this-&gt;creditHours = 0.0;
        this-&gt; numOfStudents = 0;
    }
    Course::Course(std::string newCourseCode, std::string newCourseTitle, double newCreditHours, int newNumOfStudents){
        this-&gt;courseCode = newCourseCode;
        this-&gt;courseTitle = newCourseTitle;
        this-&gt;creditHours = newCreditHours;
        this-&gt;numOfStudents = newNumOfStudents;
    }
  
    void Course::readScores()
    {
      double score;
      std::cout&lt;&lt; &quot;Enter the scores for &quot; &lt;&lt; this-&gt;numOfStudents&lt;&lt; &quot; students: \n&quot;;
      for(int i = 0; i &lt; this-&gt;numOfStudents; i++)
      {
          this-&gt;studentScores.push_back(1);
          std::cin &gt;&gt; score;
          this-&gt;studentScores[i] = score;
      }
    }
  
    double Course::getSum()
    {
      double sum = 0;
      double nextNumber;
      for(int i = 0; i &lt; studentScores.size(); i++)
      {
        nextNumber = this-&gt;studentScores[i];
        sum += nextNumber;
      }
      return sum;
    }
  
    double Course::getAverage()
    { double sum = getSum();
      double average = (sum / this-&gt;numOfStudents);
      return average;
    }
  </code></pre>