struct / class - Need pointers

Need Pointers on this, went through my book again trying to solve this question and running into brick walls

define the struct studentType to implement the basic properties of a student. Define the class studentType with the same components as the struct studentType, and add member functions to manipulate the data members. (Note that the data members of the class studentType must be private.)

Write a program to illustrate how to use the class studentType.

Struct studentType:

struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};


Test Contents

TEST(Grades, 1) {
testing::internal::CaptureStdout();
studentType student;
studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);

student.print();
cout << "***************" << endl << endl;

newStudent.print();
cout << "***************" << endl << endl;
std::string output = testing::internal::GetCapturedStdout();
ASSERT_EQ(output, "Name: \nGrade: F\nTest score: 0\nProgramming score: 0\nGPA: 0\n***************\n\nName: Brain Johnson\nGrade: A\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n");
}



// Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Main.cpp
// Build 7
//
#include <iostream>
#include <fstream>
#include "studentType.h"
using namespace std;

int main()
{
  student.print();
  cout << "***************" << endl << endl;

  newStudent.print();
  cout << "***************" << endl << endl;
    
    return 0;
}  


studentType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// studentType.h
// Build 7
//
#define STUDENTTYPE_H
#include <fstream>
#include <string>
using namespace std;

struct studentType 
{
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA; 
}; 

class studentType
{
private:
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA; 
}; 

studentType newStudent;
{
    cin >> newStudent.firstName;
    cin >> newStudent.testScore >> newStudent.programmingScore;
    score = (newStudent.testScore + newStudent.programmingScore) / 2;
    if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';
    Cout << newStudent.firstName << " " << newStudent.lastName
        << " " << newStudent.courseGrade
        << " " << newStudent.testScore
        << " " << newStudent.programmingScore
        << " " << newStudent.GPA << endl;
}
studentType student;


studentTypeImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// studentTypeImp.cpp
// Build 7
//
#include "studentType.h"
#include <iostream>
#include <fstream>
using namespace std;

if (student.firstName == newStudent.firstName && student.lastName == newStudent.lastName)

void readIn(studentType& student)
{
    int score;
    
    cin >> student.firstName >> student.lastName;
    cin >> student.testScore >> student.programmingScore;
    cin >> student.GPA;
    
    score = (student.testScore + student.programmingScore) / 2;
    
    if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';
}

void printStudent(studentType student)
{
    cout <<student.firstName << " " << student.lastName
        << " " << student.courseGrade
        << " " << student.testScore
        << " " << student.programmingScore
        << " " << student.GPA << endl;
}
It seems that this assignments wants you learn about getter and setter functions. Setter functions are used to manipulate the data while getter functions are used to fetch data. From what I learned, getter functions (in classes) are denoted with a const following the () as a way of "promising" to not modify anything within the class. After all, getters should only fetch data, not modify it.

Your class should looks something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class studentType
{
  public:
    /* Getter functions */
    string getFirstName()        const;
    string getLastName()         const;
    char   getCourseGrade()      const;
    int    getTestScore()        const;
    int    getProgrammingScore() const;

    /* Setter functions */
    string setFirstName(const std::string&);
    string setLastName(const std::string&);
    char   setCourseGrade(const char&);
    int    setTestScore(const int&);
    int    setProgrammingScore(const int&);

  private:
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA;
};

Or similar.
fiji885 (218)

If i did tht would i leave the struct student type as well since it wants to define the struct and then define the class with same components or would the struct be placed in one of the other files?
You can't have the struct and the class share the name. Are you sure they want you to implement the both the struct and class with the same name? If so, you can't unless you use namespaces. It wouldn't matter if they're in different files, since the compiler will just link all the data in the end. Classes and structs will be declared (usually) in the header files if you want to use them in other cpp files, but that's usually for bigger programs.
i should probably take the grades from the last file and only put them in the middle file?

In file included from main.cpp:6:0:
studentType.h:36:1: error: expected unqualified-id before ‘{’ token
 {
 ^
main.cpp: In function ‘int main()’:
main.cpp:11:11: error: ‘class studentType’ has no member named ‘print’
   student.print();
           ^~~~~
main.cpp:14:14: error: ‘class studentType’ has no member named ‘print’
   newStudent.print();
              ^~~~~
studentType.h:36:1: error: expected unqualified-id before ‘{’ token
 {
 ^
studentTypeImp.cpp:9:1: error: expected unqualified-id before ‘if’
 if (student.firstName == newStudent.firstName && student.lastName == newStudent.lastName)
 ^~
studentTypeImp.cpp: In function ‘void printStudent(studentType)’:
studentTypeImp.cpp:35:20: error: ‘std::__cxx11::string studentType::firstName’ is private within this context
     cout <<student.firstName << " " << student.lastName
                    ^~~~~~~~~
studentType.h:27:12: note: declared private here
     string firstName;
            ^~~~~~~~~
studentTypeImp.cpp:35:48: error: ‘std::__cxx11::string studentType::lastName’ is private within this context
 t <<student.firstName << " " << student.lastName
                                         ^~~~~~~~
studentType.h:28:12: note: declared private here
     string lastName;
            ^~~~~~~~
studentTypeImp.cpp:36:27: error: ‘char studentType::courseGrade’ is private within this context
         << " " << student.courseGrade
                           ^~~~~~~~~~~
studentType.h:29:10: note: declared private here
     char courseGrade;
          ^~~~~~~~~~~
studentTypeImp.cpp:37:27: error: ‘int studentType::testScore’ is private within this context
         << " " << student.testScore
                           ^~~~~~~~~
studentType.h:30:9: note: declared private here
     int testScore;
         ^~~~~~~~~
studentTypeImp.cpp:38:27: error: ‘int studentType::programmingScore’ is private within this context
         << " " << student.programmingScore
                           ^~~~~~~~~~~~~~~~
studentType.h:31:9: note: declared private here
     int programmingScore;
         ^~~~~~~~~~~~~~~~
studentTypeImp.cpp:39:27: error: ‘double studentType::GPA’ is private within this context
         << " " << student.GPA << endl;
                           ^~~
studentType.h:32:12: note: declared private here
     double GPA;
            ^~~
bash: line 2: ./a.out: No such file or directory
added void print(); above for the print
but im getting errors about being set to priavte
What do you mean by "middle" file? The errors that you are getting are because you trying to access private data. This is why you have setter and getter functions, as they will access the data for you because they are part of the class.

Also, I made a mistake. The return type for the setters should be void (I was sloppy copying-pasting from the getters functions and made a small mistake).

These are your setter functions
1
2
3
4
5
6
    /* Setter functions */
    void setFirstName(const std::string&);
    void setLastName(const std::string&);
    void setCourseGrade(const char&);
    void setTestScore(const int&);
    void setProgrammingScore(const int&);


In your cpp file for the studentType, it should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "studentType.h"

/* In the cpp file, you will implement the functions of the class from
 * studentType.h
 *     Getter functions
 *  string getFirstName()        const;
 *  string getLastName()         const;
 *  char   getCourseGrade()      const;
 *  int    getTestScore()        const;
 *  int    getProgrammingScore() const;
 *
 *     Setter functions
 *  void setFirstName(const std::string&);
 *  void setLastName(const std::string&);
 *  void setCourseGrade(const char&);
 *  void setTestScore(const int&);
 *  void setProgrammingScore(const int&);
 *
 */

/* For example, to the getter function for first name will be */
string studentType::getFirstName() const {
  return firstName;
}

/* And the setter function for the first name will be */
void studentType::setFirstName(const std::string& s) {
  firstName = s;
}
Last edited on
if i implement that should i scrap the other information i already had placed in my studentTypeImp.cpp?
Yes. Scratch it and reimplement it. A lot of it should be scrapped, but some parts can be reused like this part:
1
2
3
4
5
6
7
8
9
10
if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';

You may have to modify it a bit.

In the end, I think you are trying to aim for something like this in your cpp file that contains the main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "studentType.h"
#include <iostream>

using namespace std;

/* You can implement the print function here and use the getter functions to
 * print the student information, or you can define a print function within the
 * class where you can access the private members directly and there do not
 * have to use the getter functions. */
void print(studentType& s) {
  cout << "Student" << endl
       << "-------" << endl
       << "First name: " << s.getFirstName() << endl
       << "Last name: " << s.getLastName() << endl
       << "Course grade: " << s.getCourseGrade() << endl
       << "Test score: " << s.getTestScore() << endl
       << "Programming score: " << s.getProgrammingScore() << endl;
}

int main() {
  studentType student;
  student.setFirstName("Richard");
  student.setLastName("Feynman");

  /* The setCourseGrade doesn't really have to be here. If you can find a way
   * to set the course grade from what the student scores are, that would also
   * work (which you get the idea as you have demonstrated above). */
  student.setCourseGrade('A');

  student.setTestScore(100);
  student.setProgrammingScore(100);
  print(student);

  /* If you have a user defined constructor, you can also do this */
  studentType student2("Bjarne", "Stroustrup", 'A', 95, 100);
  print(student2);

  return 0;
}


with its output being (similar to):

$ ./a.out 
Student
-------
First name: Richard
Last name: Feynman
Course grade: A
Test score: 100
Programming score: 100
Student
-------
First name: Bjarne
Last name: Stroustrup
Course grade: A
Test score: 95
Programming score: 100
$ 
heres what i got now and output for it

Main.cpp (file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Build 7
//
#include <iostream>
#include <fstream>
#include "studentType.h"
using namespace std;

int main() {
  studentType student;
  student.setFirstName("Brain");
  student.setLastName("Johnson");

  student.setCourseGrade('*');

  student.setTestScore(85);
  student.setProgrammingScore(95);
  print(student);

  studentType student2("Brain", "Johnson", 'A', 85, 95);
  print(student2);

  return 0;
}


studentType.h (file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Build 7
//
#define STUDENTTYPE_H
#include <fstream>
#include <string>
using namespace std;

class studentType
{
    
  public:
    // Getter functions 
    string getFirstName()        const;
    string getLastName()         const;
    char   getCourseGrade()      const;
    int    getTestScore()        const;
    int    getProgrammingScore() const;


    // Setter functions 
    void setFirstName(const std::string&);
    void setLastName(const std::string&);
    void setCourseGrade(const char&);
    void setTestScore(const int&);
    void setProgrammingScore(const int&);


  private:
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA;
};

studentType newStudent;
{
    cin >> newStudent.firstName;
    cin >> newStudent.testScore >> newStudent.programmingScore;
    score = (newStudent.testScore + newStudent.programmingScore) / 2;
    if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';
    Cout << newStudent.firstName << " " << newStudent.lastName
        << " " << newStudent.courseGrade
        << " " << newStudent.testScore
        << " " << newStudent.programmingScore
        << " " << newStudent.GPA << endl;
    
}
studentType student;


studentTypeImp.cpp (file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Build 7
//
#include "studentType.h"
#include <iostream>
#include <fstream>
using namespace std;
int score;
score = (student.testScore + student.programmingScore) / 2;
if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';

void print(studentType& s) 
{
    cout << "Name: " << s.getFirstName() << " " << s.getLastName() << '\n'
         << "Grade: " << s.getCourseGrade() << '\n'
         << "Test score: " << s.getTestScore() << '\n'
         << "Programming score: " << s.getProgrammingScore() << '\n'
         << "GPA: " << GPA << '\n';
    cout << "***************" << endl << endl;
    
}


Output

In file included from /root/sandboxd8237062/studentTypeImp.cpp:4:0,
                 from /root/sandboxd8237062/nt-test-a1086e1d.cpp:2:
/root/sandboxd8237062/studentType.h:39:1: error: expected unqualified-id before '{' token
 {
 ^
In file included from /root/sandboxd8237062/nt-test-a1086e1d.cpp:2:0:
/root/sandboxd8237062/studentTypeImp.cpp:9:1: error: 'score' does not name a type
 score = (student.testScore + student.programmingScore) / 2;
 ^~~~~
/root/sandboxd8237062/studentTypeImp.cpp:10:1: error: expected unqualified-id before 'if'
 if (score >= 90)
 ^~
/root/sandboxd8237062/studentTypeImp.cpp:12:5: error: expected unqualified-id before 'else'
     else if (score >= 80)
     ^~~~
/root/sandboxd8237062/studentTypeImp.cpp:14:5: error: expected unqualified-id before 'else'
     else if (score >= 70)
     ^~~~
/root/sandboxd8237062/studentTypeImp.cpp:16:5: error: expected unqualified-id before 'else'
     else if (score >= 60)
     ^~~~
/root/sandboxd8237062/studentTypeImp.cpp:18:5: error: expected unqualified-id before 'else'
     else
     ^~~~
/root/sandboxd8237062/studentTypeImp.cpp: In function 'void print(studentType&)':
/root/sandboxd8237062/studentTypeImp.cpp:27:24: error: 'GPA' was not declared in this scope
          << "GPA: " << GPA << '\n';
                        ^~~
/root/sandboxd8237062/nt-test-a1086e1d.cpp: In member function 'virtual void Grades_1_Test::TestBody()':
/root/sandboxd8237062/nt-test-a1086e1d.cpp:6:63: error: no matching function for call to 'studentType::studentType(const char [6], const char [8], char, int, int, double)'
   studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);
                                                               ^
In file included from /root/sandboxd8237062/studentTypeImp.cpp:4:0,
                 from /root/sandboxd8237062/nt-test-a1086e1d.cpp:2:
/root/sandboxd8237062/studentType.h:9:7: note: candidate: studentType::studentType()
 class studentType
       ^~~~~~~~~~~
/root/sandboxd8237062/studentType.h:9:7: note:   candidate expects 0 arguments, 6 provided
/root/sandboxd8237062/studentType.h:9:7: note: candidate: studentType::studentType(const studentType&)
/root/sandboxd8237062/studentType.h:9:7: note:   candidate expects 1 argument, 6 provided
/root/sandboxd8237062/studentType.h:9:7: note: candidate: studentType::studentType(studentType&&)
/root/sandboxd8237062/studentType.h:9:7: note:   candidate expects 1 argument, 6 provided
/root/sandboxd8237062/nt-test-a1086e1d.cpp:8:11: error: 'class studentType' has no member named 'print'
   student.print();
           ^~~~~
/root/sandboxd8237062/nt-test-a1086e1d.cpp:11:14: error: 'class studentType' has no member named 'print'
   newStudent.print();
              ^~~~~
make[2]: *** [CMakeFiles/runTests.dir/nt-test-a1086e1d.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
BTW i had a original working post posted at
http://www.cplusplus.com/forum/beginner/252211/
Now this one was working but not passing the argument, could their be any possible way to modify this one with the work above ?
First, let's take a look at your header file. In your studentType.h
file, you should have
1
2
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H 


and at the end of the header file, you'll want to have an #endif .

On line 37, you are instantiating an object of the class studentType, which is
not what you're suppose to be doing. Can you explain to me what you're trying
to accomplish there so I can help you? Same thing going on with line 59. Lines
38 through 58 will not do anything useful. I can see what you're trying to do
there, but how you're implementing it is wrong. You should use a function
instead of doing what you're doing.



Now, let's take a look at your studentTypeImp.cpp file. On line 7, you have a
global variable. Remove it. I don't know why it's there. The studentTypeImp.cpp
file is suppose to be where you implement the functions from the class (look at
my example from above). Why do you have the fstream header included? Are you
suppose to read data in from a file? You're going at the cpp file the wrong
way. Implement the class functions from the header file into the header file.

blackstar wrote:

BTW i had a original working post posted at
http://www.cplusplus.com/forum/beginner/252211/
Now this one was working but not passing the argument, could their be any possible way to modify this one with the work above ?

You can reuse some parts, but you may have to adjust them to work. For example,
the user-defined constructor for the struct can be used again to work with the
class.
1
2
3
4
5
6
7
8
9
studentType :: studentType (const string &firstName, const string &lastName, char courseGrade, int testScore, int programmingScore, double GPA )
{
    this -> testScore = testScore;
    this -> firstName = firstName;
    this -> lastName = lastName;
    this -> GPA = GPA;
    this -> courseGrade = courseGrade;
    this -> programmingScore = programmingScore;
}

i oddly feel like im onto something here but confused as well. (mabye im going insane now) lol staring at this for the last 20 hrs
Took the old code and am currently modifying it

keep staring at the test output

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Grades
[ RUN      ] Grades.1
/root/sandbox1014ee0d/nt-test-b5843ca6.cpp:14: Failure
Value of: "Name:  \nGrade: F\nTest score: 0\nProgramming score: 0\nGPA: 0\n***************\n\nName: Brain Johnson\nGrade: A\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n"
Expected: output
Which is: "Name:    \nGrade:  \nTest score: 0\nProgramming score: 32\nGPA: 0\n***************\n\nName: Brain Johnson\nGrade: *\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n"
[  FAILED  ] Grades.1 (0 ms)
[----------] 1 test from Grades (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Grades.1

 1 FAILED TEST


if i keep staring at it im noticing some things especially here like mabye this shows what needs to be private/public/modified?

Value of: 
Name:  \nGrade: F\nTest score: 0\nProgramming score: 0\nGPA: 0\n***************\n\n
Name: Brain Johnson\nGrade: A\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n

Expected: output
Which is: 
Name:    \nGrade:  \nTest score: 0\nProgramming score: 32\nGPA: 0\n***************\n\n
Name: Brain Johnson\nGrade: *\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n"


the test contents are noted as this also

TEST(Grades, 1) {
  testing::internal::CaptureStdout();
  studentType student;
  studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);

  student.print();
  cout << "***************" << endl << endl;

  newStudent.print();
  cout << "***************" << endl << endl;
  std::string output = testing::internal::GetCapturedStdout();
  ASSERT_EQ(output, "Name:  \nGrade: F\nTest score: 0\nProgramming score: 0\nGPA: 0\n***************\n\nName: Brain Johnson\nGrade: A\nTest score: 85\nProgramming score: 95\nGPA: 3.89\n***************\n\n");
}


now all that is based on this current code, so idk if the program is actually inputting or just trying to read and get the correct data out of it like a database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// main.cpp
// Build 8
#include <iostream>
#include <fstream>
#include "studentType.h"
using namespace std;

int main()
{

    studentType student;

    cout << "Name: ";
    getline (cin, student.firstName);

    cout << "Grade: ";
    cin >> student.courseGrade;
    
    cout << "Test score: ";
    cin >> student.testScore;
    
    cout << "Programming score: ";
    cin >> student.programmingScore;
    
    cout << "GPA: ";
    cin >> student.GPA;
    studentType newStudent("Brain", "Johnson", 'A', 85, 95, 3.89);
    
    student.print();
    cout << "***************" << endl << endl;
    
    newStudent.print();
    cout << "***************" << endl << endl;
    
    return 0;
}  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// studentType.h
// Build 8
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H
#include <fstream>
#include <string>
using namespace std;
struct studentType 
{
        studentType (const string &firstName = " ", const string &lastName = " ", char courseGrade = ' ', int testScore = 0, int programmingScore = ' ', double GPA = 0.0f );
        void setstudentType(int testScore, string firstName, string lastName, double GPA, char courseGrade, int programmingScore);
        void print();
public:
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA; 

};
#endif  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// studentTypeImp.cpp
// Build 8
#include "studentType.h"
#include <iostream>
#include <fstream>
using namespace std;


studentType :: studentType (const string &firstName, const string &lastName, char courseGrade, int testScore, int programmingScore, double GPA )
{
    this -> testScore = testScore;
    this -> firstName = firstName;
    this -> lastName = lastName;
    this -> GPA = GPA;
    this -> courseGrade = courseGrade;
    this -> programmingScore = programmingScore;
}

void studentType :: print()
{
    cout << "Name: " << firstName << " " << lastName << '\n'
     << "Grade: " << courseGrade << '\n'
     << "Test score: " << testScore << '\n'
     << "Programming score: " << programmingScore << '\n'
     << "GPA: " << GPA << '\n';
} 
Don't work on modifying the old code. You're just going to confuse yourself
even more. You can use some of the old code, but you will have to modify
it
. The goal here was that you turn the struct into a class and add
functions to manipulate the private data. Stick to what you had with the class.

define the struct studentType to implement the basic properties of a
student. Define the class studentType with the same components as the
struct studentType, and add member functions to manipulate the data
members.
(Note that the data members of the class studentType must be
private.)


Look at the class definiton I had given you. All you literally have to do is
implement the functions in the cpp file. I am not sure what you're using to
compile or to even test, so that output doesn't tell me much.
ok went back to the other code and modified it like you said.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// main.cpp
// Build 9
//
#include <iostream>
#include "studentType.h"
using namespace std;

int main() {
  studentType student;
  student.setFirstName("Brain");
  student.setLastName("Johnson");

  student.setCourseGrade('*');

  student.setTestScore(85);
  student.setProgrammingScore(95);
  print(student);

  studentType student2("Brain", "Johnson", 'A', 85, 95);
  print(student2);

  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// studentType.h
// Build 9
//
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H
#include <string>
using namespace std;

class studentType
{
    
  public:
    // Getter functions 
    string getFirstName()        const;
    string getLastName()         const;
    char   getCourseGrade()      const;
    int    getTestScore()        const;
    int    getProgrammingScore() const;


    // Setter functions 
    void setFirstName(const std::string&);
    void setLastName(const std::string&);
    void setCourseGrade(const char&);
    void setTestScore(const int&);
    void setProgrammingScore(const int&);


  private:
    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA;
};
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// studentTypeImp.cpp
// Build 9
//
#include "studentType.h"
#include <iostream>
using namespace std;
score = (student.testScore + student.programmingScore) / 2;
if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';

void print(studentType& s) 
{
    cout << "Name: " << s.getFirstName() << " " << s.getLastName() << '\n'
         << "Grade: " << s.getCourseGrade() << '\n'
         << "Test score: " << s.getTestScore() << '\n'
         << "Programming score: " << s.getProgrammingScore() << '\n'
         << "GPA: " << GPA << '\n';
    cout << "***************" << endl << endl;
    
}


main.cpp: In function ‘int main()’:
main.cpp:17:3: error: ‘print’ was not declared in this scope
   print(student);
   ^~~~~
main.cpp:17:3: note: suggested alternative: ‘printf’
   print(student);
   ^~~~~
   printf
main.cpp:19:55: error: no matching function for call to ‘studentType::studentType(const char [6], const char [8], char, int, int)’
   studentType student2("Brain", "Johnson", 'A', 85, 95);
                                                       ^
In file included from main.cpp:5:0:
studentType.h:9:7: note: candidate: studentType::studentType()
 class studentType
       ^~~~~~~~~~~
studentType.h:9:7: note:   candidate expects 0 arguments, 5 provided
studentType.h:9:7: note: candidate: studentType::studentType(const studentType&)
studentType.h:9:7: note:   candidate expects 1 argument, 5 provided
studentType.h:9:7: note: candidate: studentType::studentType(studentType&&)
studentType.h:9:7: note:   candidate expects 1 argument, 5 provided
studentTypeImp.cpp:7:1: error: ‘score’ does not name a type; did you mean ‘short’?
 score = (student.testScore + student.programmingScore) / 2;
 ^~~~~
 short
studentTypeImp.cpp:8:1: error: expected unqualified-id before ‘if’
 if (score >= 90)
 ^~
studentTypeImp.cpp:10:5: error: expected unqualified-id before ‘else’
     else if (score >= 80)
     ^~~~
studentTypeImp.cpp:12:5: error: expected unqualified-id before ‘else’
     else if (score >= 70)
     ^~~~
studentTypeImp.cpp:14:5: error: expected unqualified-id before ‘else’
     else if (score >= 60)
     ^~~~
studentTypeImp.cpp:16:5: error: expected unqualified-id before ‘else’
     else
     ^~~~
studentTypeImp.cpp: In function ‘void print(studentType&)’:
studentTypeImp.cpp:25:24: error: ‘GPA’ was not declared in this scope
          << "GPA: " << GPA << '\n';
                        ^~~
bash: line 2: ./a.out: No such file or directory

Look. In your studentTypeImp.cpp implement the functions for the class studentType.

In your studentTypeImp.cpp explain to me your thought process when doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
score = (student.testScore + student.programmingScore) / 2;
if (score >= 90)
        newStudent.courseGrade = 'A';
    else if (score >= 80)
        newStudent.courseGrade = 'B';
    else if (score >= 70)
        newStudent.courseGrade = 'C';
    else if (score >= 60)
        newStudent.courseGrade = 'D';
    else
        newStudent.courseGrade = 'F';

void print(studentType& s) 
{
    cout << "Name: " << s.getFirstName() << " " << s.getLastName() << '\n'
         << "Grade: " << s.getCourseGrade() << '\n'
         << "Test score: " << s.getTestScore() << '\n'
         << "Programming score: " << s.getProgrammingScore() << '\n'
         << "GPA: " << GPA << '\n';
    cout << "***************" << endl << endl;
    
}


I've said in previous posts that you have to implement the functions in the
cpp file
. You've done none of it. I even provided you an example. From what
it seems, you don't don't really seem to know how basic classes work.

http://www.cplusplus.com/doc/tutorial/classes/

Read up on this before trying to do anything further.
sorry
fiji885 (225)

ill read over the classes again, my brain is all over the place and i dont really get all over it, specially getten confused bc i got 2 .cpp files
its how the grade compiler works on the cenage site.
ill read over your class link again and then read over my chapter again on classes and see if i can put something more together
and thanks for helping a dummy like me, i know its frustrating XD
Topic archived. No new replies allowed.