Help with code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lafayettejohnson
    New Member
    • Jul 2010
    • 7

    #1

    Help with code

    there are 3 files but the one iam having problems with is the student.cpp



    student.cpp
    Code:
    #include <iostream>    // for ostream << and istream >>
    #include <string> 
    #include "student.h"
    using namespace std;
    
    //--constructors
    
    student::student()
    {
      my_name = "?name?";
      my_credits = 0.0;
      my_qualityPoints = 0.0;
    }
    
    student::student(string initName, double initCredits, double initQualityPoints)
    {
      my_name = "initName,";
      my_credits = initCredits;
      my_qualityPoints = initQualityPoints;
    }
    
    //--modifiers
    
    void student::completedCourse(double credits, double numericGrade)
    {
    my_credits = my_credits + credits;
    my_qualityPoints = my_qualityPoints + (my_credits * numericGrade);
    }
    
    
    //--accessors
    double student::GPA() const
    {
     my_qualityPoints / my_credits;
      return;
    }
    string student::standing() const
    {
    if credits > 30 
      return “Freshman”;
    if credits >= 30
    return “Sophomore”;
    if credits >= 60
    return “Junior”;
    if credits >=90
    return “Senior”;
    
    }
    string student::name() const
    {
    Return my_name;
    }
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "student.h" // this includs student.h
    #include "compfun.h"     // for decimals
    
    using namespace std;
    
    void display(student aStudent)
    {
      decimals(cout, 2);
      cout << "{ student: " << aStudent.name();
      cout << ", GPA = " << aStudent.GPA() << " }" << endl;
    }
    
    int main()
    { // test drivbe student: this main will vary a,mongst students
      student aStudent("Nguyen", 36.5, 123.5);
      student anotherStudent("Stella", 4.0, 16.0);  // Straight A so far
      student one ("one should be 3.0", 0.0, 0.0);
      one.completedCourse( 4.0, 2.0 );
      one.completedCourse( 4.0, 4.0 );  // 4 credit A
      display( one );
      display( aStudent );
      display( anotherStudent );
    
      // Finish branch coverage testing of standing
      student two("two", 100.0, 30.0);
      student three( "three", 30.05, 100.0 );
      student four("four" , 60.0, 100.0 );
      student five("five ", 60.05, 100.0);
      student six ("six " , 90.0, 100.0);
      student seven("seven", 90.05, 100.0 );
    
      cout << one.standing() << endl;
      cout << two.standing() << endl;
      cout << three.standing() << endl;
      cout << four.standing() << endl;
      cout << five.standing() << endl;
      cout << six.standing() << endl;
      cout << seven.standing() << endl;
    
      return 0;
    }
    
    
    [B]student.h[/B]
    [CODE]
    #ifndef STUDENT_H
    #define STUDENT_H

    #include <iostream>
    #include <string>
    using namespace std;

    class student {
    public:
    //--constructors
    student();
    // post: Initialize a student object with a name as "?name?,"
    // 0.0 credits, and 0.0 quality points

    student(string initName, double initCredits, double initQualityPoin ts);
    // post: Initialize a student object with this 3 argument constructor
    // student("Delais io, Donna," 30.0, 120.0);
    // A straight A sophomore at one school

    //--modifier
    void completedCourse (double credits, double numericGrade);
    // post: record a completed course by adding credits to my_credits
    // and incrementing the qualityPoints by (credits * numericGrade)
    // aStudent.comple tedCourse(4.0, 3.67) // a 4 credit A- perhaps

    //--accessors
    double GPA() const;
    // post: return the current grade point average as the accumulated
    // quality points divided by the total number of credits.

    string standing() const;
    // post: use selection to return the current standing as either
    // Freshman, Sophomore, Junior, or Senior.

    string name() const;
    // post: return the student's name

    private:
    string my_name;
    double my_credits; // Total credits completed
    double my_qualityPoint s; // sum of credits multiplied by grades
    };

    #endif

    [/CODE]
    Last edited by Niheel; Jul 21 '10, 04:47 AM.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please describe the problems you're having with student.cpp.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      From a quick, simple inspection, I see the following two issues with student.cpp:

      student::GPA() has a return statement with no value.
      student::standi ng() has a code path which does not encounter a return statement.

      Try resolving these issues and then give us a better description of the problems you're having.

      Also, please use code tags. They makes reading source code sooooo much easier.

      Comment

      • lafayettejohnson
        New Member
        • Jul 2010
        • 7

        #4
        I am still having Problems getting the student.cpp to compile.

        The first error I am getting is C2065: '“Freshman”‘: undeclared identifier. Any more help would be greatly appreciated, if you can’t tell I am new to this c++ programming and this summer class is killing me.

        student.cpp
        Code:
        #include <iostream>    // for ostream << and istream >>
        #include <string> 
        #include "student.h"
        using namespace std;
        
        
        
        student::student()
        {
          my_name = "?name?";
          my_credits = 0.0;
          my_qualityPoints = 0.0;
        }
        
        student::student(string initName, double initCredits, double initQualityPoints)
        {
          my_name = initName;
          my_credits = initCredits;
          my_qualityPoints = initQualityPoints;
        }
        
        //--modifiers
        
        void student::completedCourse(double credits, double numericGrade)
        {
        my_credits = my_credits + credits;
        my_qualityPoints = my_qualityPoints + (credits * numericGrade), credits * numericGrade;
        }
        
        
        //--accessors
        double student::GPA() const
        {
        	double results;
        results = my_qualityPoints / my_credits;
          return results;
        }
        string student::standing() const
        {
        	
        if( my_credits < 30) 
          return “Freshman”;
        if (my_credits >= 30)
        return “Sophomore”;
        if (my_credits >= 60)
        return “Junior”;
        if (my_credits >=90)
        return “Senior”;
        else
        return 0;
        }
        string student::name() const
        {
        return my_name;
        }
        main.cpp
        Code:
        #include <iostream>
        #include <string>
        #include "student.h" // this includs student.h
        #include "compfun.h"     // for decimals
        
        using namespace std;
        
        void display(student aStudent)
        {
          decimals(cout, 2);
          cout << "{ student: " << aStudent.name();
          cout << ", GPA = " << aStudent.GPA() << " }" << endl;
        }
        
        int main()
        { // test drivbe student: this main will vary a,mongst students
          student aStudent("Nguyen", 36.5, 123.5);
          student anotherStudent("Stella", 4.0, 16.0);  // Straight A so far
          student one ("one should be 3.0", 0.0, 0.0);
          one.completedCourse( 4.0, 2.0 );
          one.completedCourse( 4.0, 4.0 );  // 4 credit A
          display( one );
          display( aStudent );
          display( anotherStudent );
        
          // Finish branch coverage testing of standing
          student two("two", 100.0, 30.0);
          student three( "three", 30.05, 100.0 );
          student four("four" , 60.0, 100.0 );
          student five("five ", 60.05, 100.0);
          student six ("six " , 90.0, 100.0);
          student seven("seven", 90.05, 100.0 );
        
          cout << one.standing() << endl;
          cout << two.standing() << endl;
          cout << three.standing() << endl;
          cout << four.standing() << endl;
          cout << five.standing() << endl;
          cout << six.standing() << endl;
          cout << seven.standing() << endl;
        
          return 0;
        }
        
        
        [B]student.h[/B]
        [CODE]
        #ifndef STUDENT_H
        #define STUDENT_H

        #include <iostream>
        #include <string>
        using namespace std;

        class student {
        public:
        //--constructors
        student();
        // post: Initialize a student object with a name as "?name?,"
        // 0.0 credits, and 0.0 quality points

        student(string initName, double initCredits, double initQualityPoin ts);
        // post: Initialize a student object with this 3 argument constructor
        // student("Delais io, Donna," 30.0, 120.0);
        // A straight A sophomore at one school

        //--modifier
        void completedCourse (double credits, double numericGrade);
        // post: record a completed course by adding credits to my_credits
        // and incrementing the qualityPoints by (credits * numericGrade)
        // aStudent.comple tedCourse(4.0, 3.67) // a 4 credit A- perhaps

        //--accessors
        double GPA() const;
        // post: return the current grade point average as the accumulated
        // quality points divided by the total number of credits.

        string standing() const;
        // post: use selection to return the current standing as either
        // Freshman, Sophomore, Junior, or Senior.

        string name() const;
        // post: return the student's name

        private:
        string my_name;
        double my_credits; // Total credits completed
        double my_qualityPoint s; // sum of credits multiplied by grades
        };

        #endif

        [/CODE][/QUOTE]
        I am still having Problems getting the student.cpp to compile. the first errorIam getting is C2065: '“Freshman”' : undeclared identifier. Any more help would be greatly appreciated if you cant tell Iam new to this c++ programing and this summer class is killing me.

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          I'll assume that you're seeing that error at line 42 of student.cpp.

          Looking at your code, it looks like your editor is putting balanced quotes in place of the C++ double quote.

          Your code:
          Code:
          if( my_credits < 30)  
            return “Freshman”;
          Probably should read
          Code:
          if( my_credits < 30)  
            return "Freshman";

          Comment

          Working...