Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
STUDENT.H

C++
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;

class Students
{
private:
    int studentNumber;
    string studentName;
    double GPA;
public:
    Students();
    void setStudentNumber(int);
    void setStudentName(string);
    void setGPA(double);
    string getStudentName();
    int getStudentNumber();
    void displayGPA();
};

#endif

STUDENT.CPP

C++
#include "student.h"
#include <iostream>
using namespace std;

Students::Students()
{
    studentNumber = 0;
    studentName = "-";
    GPA = 0.0;
}

void Students::setStudentNumber(int studentNumber)
{
    studentNumber = studentNumber;
}

void Students::setStudentName(string studentName)
{
    studentName = studentName;
}

void Students::setGPA(double GPA)
{
    GPA = GPA;
}

string Students::getStudentName()
{
    return studentName;
}

int Students::getStudentNumber()
{
    return studentNumber;
}

void Students::displayGPA()
{
    cout << GPA;
}

STUDENTCLASS.CPP

C++
#include "student.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    Students student1;
    Students students[3];

    cout << "------------------------------------------------------------" << endl
         << "--------------------A Single Object------------------------------" << endl;
    cout << setw(5) << left << student1.getStudentName() 
     << setw(5) << left << student1.getStudentNumber()
         << student1.displayGPA;

    student1.setStudentName("John Doe");
    student1.setStudentNumber(1);
    student1.setGPA(3.5);

    cout << "------------------------------------------------------------" << endl
     << "Student1 now has information:";
    cout << setw(10) << left << student1.getStudentName()
     << setw(10) << left << student1.getStudentNumber()
     << setw(10) << left << student1.displayGPA;

cout << "------------------------------------------------------------" << endl
     << "--------------------An Array of Objects-----------------------" << endl;

for(int x = 0; x < 3; x++)
{
    cout << "Please enter information for student" << x << ":" << endl;
    cout << "Name:";
    cin >> students[x].setStudentName;

    while(students[x].getStudentName() == "-")
    {
        cout << "Invalid input!";
        cout << "Name:";
        cin >> students[x].setStudentName;
    }

    cout << "ID: ";
    cin >> students[x].setStudentNumber;

    while(students[x].getStudentNumber() < 1)
    {
        cout << "Invalid input!";
        cout << "ID: ";
        cin >> students[x].setStudentNumber;
    }

    cout << "GPA: ";
    cin >> students[x].setGPA;

    while(students[x].displayGPA == 0)
    {
        cout << "Invalid input!";
        cout << "GPA: ";
        cin >> students[x].setGPA;
    }
}

cout << "------------------------------------------------------------" << endl
     << setw(10) << left << "Name"
     << setw(10) << left << "Number" 
     << setw(10) << left << "GPA" << endl;

for(int x = 0; x < 3; x++)
{
    cout << students[x].getStudentName();
    cout << students[x].getStudentNumber();
    cout << students[x].displayGPA;
}

return 0;
}

Please help... I keep getting errors with the displayGPA function and a binary error that >> with the cin does not work. Any guidance would be much appreciated. My teacher apparently doesn't answer her email and I desperately need help.
Posted

This function
void Students::displayGPA()
{
    cout << GPA;
}


should be

double Students::displayGPA()
{
    return GPA;
}
 
Share this answer
 
Comments
Member 11442883 10-Feb-15 19:26pm    
On the instruction sheet given by the teacher it says "displayGPA - void function, display GPA.". It works by using a double and returning the GPA but not the way she wants.

I also have the problem of the binary '>>' remaining too );
1. Why displayGPA()? Just change that to
C++
double getGPA()
[Edit} OK keep displayGPA()

cout << displayGPA()

will result in:

C++
cout << (cout << GPA);

just do:

C++
for(int x = 0; x < 3; x++)
{
    cout << students[x].getStudentName();
    cout << students[x].getStudentNumber();
    students[x].displayGPA;
    cout << endl;
}


2. You will need temporary variables to use cin.

e.g.

C++
 string studentName;

cin >> studentName;

students[x].setStudentName(studentName);


That will require some reworking but come back once that is done.
 
Share this answer
 
v4
Comments
Stefan_Lang 12-Feb-15 7:21am    
cout << displayGPA() is invalid because the function returns void, and void is not a valid argument for operator<< An inline version would look like this (it would be equivalent if GPA were a public member):
cout << (cout << students[x].GPA , void)
The former version would work if displayGPA() would return an ostream reference like this:
ostream& displayGPA() {
return cout << GPA;
}

but apparently the instructions specifically void that option (pun intended)
[no name] 12-Feb-15 8:10am    
Thanks for the insight. It is a neat solution. It would make a much better question because returning a void is pretty uninteresting.
Quote:
<< setw(10) << left << student1.displayGPA;

As pointed out above, you can't chain the call to displayGPA into the output stream like that. This poses a problem: you obviously want a specific format, but even if you call displayGPA() correctly, the format specification is done outside the student class, and will be forgotten within the display function!

You stated that your instructions require that displayGPA() returns void. Given that, if you want that formatting. you need to find another way:
1. pass the format specification (alignment, spacing) as additional parameters to your displayGPA() function and apply the formatting within the function
2. add another function to specify the formatting for any display function and store the formatting specifiers within your student class (not a great solution imho. The student class shouldn't have to concern itself with display format info)
3. Add another function to get the GPA value directly rather than having it printed (the only reasonable solution imho)

Quote:
while(students[x].getStudentName() == "-")

Here are some big issues:
1. you are specifically checking for a default value defined within the student class. How is the program using a class supposed to know of that value in general, assuming that the function implementation is in a different source file, or may even only be vailable in binary format as a lib or dll?
1.a) you are also equating that default with an invalid input, which is plain wrong: your loop will accept any input that isn't "-", even an empty string, a string containg numbers or other non-alphabetic characters, all of which are clearly invalid!
2. the way you ask for input and check it's validity are things that only the student class should need to know about, not your main program! This is clearly code that belongs into the student class
3. You (try to) directly stream input from cin into a variable. Apart from the fact that the code is incorrect, you should normally never directly stream input into the final destination variable. How do you know the input is a valid string? How do you know it doesn't somehow cause an issue? Are you willing to overwrite an existing valid value with one that is complete gibberish without checking? A better way is to read the input into a temporary variable, check the validity of that variable, and only then copy it to the true destination. Again, only the student class should need to know how to best handle that, and provide functions for reading these values from an input stream.

Quote:
while(students[x].getStudentNumber() < 1)

Same problem: you are using internal knowledge of the student class, i. e. that a value < 1 is invalid. Leave that code to the student class! Provide a function for reading the student number and checking it properly. Besides, classes are all about reusability. A well implemented class may be used in dozens or hundreds of places within a program. If some time in the future the student format changes to allow for alphanumeric IDs in place of simple numbers, would you rather change your code in hundreds of places - or just within the student class? ;)
Quote:
while(students[x].displayGPA == 0)

Do I need to mention this is the same problem as above? What's more, the restrictionsin your instructions on the function displayGPA() make it impossible to actually check a valid input! Again, put that code inside a function in the student class, and you're fine.


Summary:
1. Add a GetGPA() function to your student class
2. Write member functions for your student class to read their own member variables from cin and do all the querying and validation within those functions
3. Always use temporary variables to hold input from cin. Check before copying them over to the actual destination.

(and you might also want to insert a few more <<endl ;) )
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900