Hello! Well, I've been given a fairly difficult assignment (at least to me), and I can't seem to wrap my head around some parts.

Here are the objectives:

"In this assignment you will demonstrate your ability to design a class and provide its declaration (.h) and implementation (.cpp) files. All activity relating to a Student object must be handled by the class. You must use a static variable to keep count of the actual number of students (not the number of objects in the array). Finally, use a stringstream to create the student display string."

Also,

"Declare a Student class that contains: student Id, first name, last name, five test scores, grade average, and course letter grade. Also declare an array of a maximum of 25 Student objects. Prompt the user and read the student data from the keyboard. You may clear the screen between student entries (system(“cls”)). Store the information in the array. When there are no more entries (EOF), clear the screen and then loop through the array to print each student. Calculate the average score, and determine the letter grade for each student. Finally display an average score for each test and for the entire class. Display the information in tabular form on the screen, one line for each student (including the average score and GPA) and a total line for the average score for each test and class. Don’t forget appropriate headings."

Okay, now for what I've written so far and what I'm having trouble with!

First, the header file:

using namespace std;
#ifndef STUDENT_CLASS
#define STUDENT_CLASS
Class Student
{
      private:
              int x;
              int studentId;
              string firstName;
              string lastName;
              int scores[5];
              double avg;
              char grade;
              static int count;
              
      public:
            Student(); 
            Student(int sX, string fN, string lN);
                    
            void setX(int i);        
            void setId(int sX);
            void setFirst(string fN);
            void setLast(string lN);
            void setAverage(double sA);
            void setGrade(char sG);
            void setScores(int sS, int y);
            
            int getId(void) const;
            string getFirst(void) const;
            string getLast(void) const;
            int getScores(void) const;
            double getAverage(void) const;
            char getGrade(void) const;
            
            
            string displayData(void) const;
};
#endif

Second, the implementation:

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

#include "Student.h"

Student :: Student()
{
        sX = 0;
        fN = "";
        lN = "";
        for(int i = 0, i < 5, i++)
        scores[i] = {0};
        double sA = 0.0;
        char sG = ' ';
}

string Student :: displayData(void)
{
       calcAverage();
       setGrade();
       
       stringstream strstr;
}

//================================Set Mutator Function declarations=============================================
void Student :: setX(int i)
{
     x = i
}

void Student :: setId(int sX)
{
     studentId = sX;
}

void Student :: setFirst(string fN)
{
     firstName = fN;
}

void Student :: setLast(string lN)
{
     lastName = lN;
}

void Student :: setAverage(double sA)
{
     avg = sA;
}

void Student :: setGrade(char sG)
{
     grade = sG;
}

void Student :: setScores(int sS, int y)
{
     scores[y] = sS;
}
//================================Get Accessor Function declarations=============================================
int Student :: getId(void) const
{
    return studentId;
}

string Student :: getFirst(void) const
{
       return firstName;
}

string Student :: getLast(void) const
{
       return lastName;
}

double Student :: getAverage(void) const
{
       return avg;
}

char Student :: getGrade(void) const
{
     return grade;
}

And lastly, main:

#include <cstdlib>
#include <iostream>

using namespace std;

const int MAX = 25;

void getData
bool getStudent(Student& userData);
void processStudents
void writeAverages

int main(int argc, char *argv[])
{
    Student userData[MAX];
    int i;
    
    for(i = 0, i < MAX, i++)
    userData[i]->setX[i];
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

bool getStudent(Student& userData)
{
     cout << "=======================New Student=======================" << endl;
     cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
     cout << "Enter student ID: ";
     cin >> sX;
     
     if(cin.eof() || cin.fail())
        return true;
        cin.ignore();
        
     cout << "Enter student's first name: ";
     getline(cin, fN);
     cout << "Enter student's last name: ";
     getline(cin, lN);
     
     userData.setId(sX);
     userData.setFirst(fN);
     userData.setLast(lN);
     
     cout << "Please enter 5 test scores (seperate with spaces): ";

     for(int i = 0; i < 5; i++)
           cin >> Student::userData.scores[i];
     return false;
}

Okay, obviously string stream is a problem here as I haven't even created a function to display the data yet. I have hardly any knowledge of string stream, but that's not that big of a deal at the moment.

The main question I have is how would I write a while loop in main to keep a count of each time the user finishes inputting a full student's data? Something like having the loop check to see if the user has input all of the information of one student, if they have add one to the count and move to the next object and if not then do not add to the count.

I am fairly weak when it comes to arrays, so hopefully you guys could clear some of this up for me! I am NOT fishing for answers here, I'd like to learn with your help. Thank you in advance!

First of all, I'd like to congratulate you on making a good post, we don't see it too often anymore around here!

Firstly, I would rename 'getStudent' to 'newStudent' or something simmilar, I think it reflects the purpose more accurately.

Now, newStudent is responsible for gathering all the information needed to create a new student, imho it should also be responsible for increasing the student count.

Q: When to increase the count?
A: When you have all the information, e.g. after getting the 5 grades.

The count is a static variable in the student class, so you can simply increase it with:

Student::count++;

You will however get a link error with the above source, having to do with the static count variable... but I don't want to spoil finding the solution for you just yet ;).

Now, you want to construct a loop in main that asks the user for student structures, until the max number of 25 is reached or until the user is finished.

I would personally write something like:

for( int i = 0; i < 25; ++i )
{
    if( false == newStudent( userData[0] ) )
         break;
     char cont;
     cout << "Add another student? [y/n]";
     cin >> cont;
     if( cont == .... etc )
}

I hope that helps.

You can add some if( i == 24 ) in the for loop, and tell the user that the maximum number of students has been reached.


Some general comments to your code:

In C++ it is not considered good practice to write 'void' when a function takes no arguments, e.g.:

int myFunc();
// Not:
int myFunc( void );

Next, what is wrong with this line?

cin >> Student::userData.scores[i];

Finally, you must have better error checking in your newStudent function, for instance in the above line you expect a score, so an integer value. What if I enter 'passed!' as a user? Certainly that's not a correct grade.

Finally #2, you use some undeclared variables in newStudent, like sX, fN, lN.

Finally #3, why does your newStudent return false at the end, and true when something went wrong? If I call a function that returns a boolean, I would expect it to return true if everything went A-Ok.

Wow, great response thank you!

I'll get to work ASAP. :)

Okay I started working on it and I am now, again, at a wall.

Here's the slightly updated code:

Header:

using namespace std;
#ifndef STUDENT_CLASS
#define STUDENT_CLASS
class Student
{
      private:
              int x;
              int studentId;
              string firstName;
              string lastName;
              int scores[5];
              double avg;
              char grade;
              
              static int count;
              
              void getAverage(void) const;
              
      public:
            Student(); 
            Student(int sX, string fN, string lN);
                    
            void setX(int i);        
            void setId(int sX);
            void setFirst(string fN);
            void setLast(string lN);
            void setAverage(double sA);
            void setGrade(char sG);
            void setScores(int sS);
            
            int getId(void) const;
            string getFirst(void) const;
            string getLast(void) const;
            int getScores(void) const;
            char getGrade(void) const;
            
            static int getCount();
            static void addCount();
            
            string displayData(void) const;
};
#endif

Implementation:

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

#include "Student.h"

int Student :: count = 0;

Student :: Student()
{
        sX = 0;
        fN = "";
        lN = "";
        for(int i = 0, i < 5, i++)
        scores[i] = {0};
        double sA = 0.0;
        char sG = ' ';
}

string Student :: displayData(void)
{
       calcAverage();
       setGrade();
       
       stringstream strstr;
}

void Student :: addCount();
{
     count++;
}

//================================Set Mutator Function declarations=============================================
void Student :: setX(int i)
{
     x = i
}

void Student :: setId(int sX)
{
     studentId = sX;
}

void Student :: setFirst(string fN)
{
     firstName = fN;
}

void Student :: setLast(string lN)
{
     lastName = lN;
}

void Student :: setAverage(double sA)
{
     avg = sA;
}

void Student :: setGrade(char sG)
{
     grade = sG;
}

void Student :: setScores(int sS)
{
     scores[i] = sS;
}
//================================Get Accessor Function declarations=============================================
int Student :: getCount(void)
{
    return count;
}

int Student :: getId(void) const
{
    return studentId;
}

string Student :: getFirst(void) const
{
       return firstName;
}

string Student :: getLast(void) const
{
       return lastName;
}

double Student :: getAverage(void) const
{
       return avg;
}

char Student :: getGrade(void) const
{
     return grade;
}

Main:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#include "Student.h"

const int MAX = 25;

void getData(Student userData[]);
bool newStudent(Student& userData);

int main(int argc, char *argv[])
{
    Student userData[MAX];
    int i;
    
    for(i = 0; i < MAX; i++)
    userData[i].setX(i);
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getData(Student userData[])
{
     bool done = false;
     int index = Student::getCount();
     while (index < MAX && !done)
     {
           while(newStudent(userData, index))
           {
                 Student::addCount();
                 index++;
           }
           done = true;
     }

}

bool newStudent(Student& userData)
{
     int sX;
     int sS;
     string fN;
     string lN;
     
     cout << "=======================New Student=======================" << endl;
     cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
     cout << "Enter student ID: ";
     cin >> sX;
     
     if(cin.eof() || cin.fail())
        return true;
        cin.ignore();
        
     cout << "Enter student's first name: ";
     getline(cin, fN);
     cout << "Enter student's last name: ";
     getline(cin, lN);
     
     userData.setId(sX);
     userData.setFirst(fN);
     userData.setLast(lN);
     
     cout << "Please enter 5 test scores (seperate with spaces): ";

     for(int i = 0; i < 5; i++)
           cin >> userData.setScores(sS);
     return false;
}

Okay I am having tons of trouble with these two lines:

while(newStudent(userData, index))

Error reads: "34 E:\Assignments\Assignment 5\main.cpp invalid initialization of reference of type 'Student&' from expression of type 'Student*'"

And

cin >> userData.setScores(sS);

Error reads: "72 E:\Assignments\Assignment 5\main.cpp no match for 'operator>>' in 'std::cin >> (+userData)->Student::setScores(sS)'"

I am not sure how to format that input or why it is not reading that loop correctly.

Also, I screwed around with that counter function. What do you think of it? I have not been able to test it out correctly yet.

Thank you!

Hmm, both errors are fairly obvious (don't take that the wrong way ;)).

Look how you declare newStudent, it takes one Student object, by reference.
In the while loop you give it a Student* and an index.

Do you understand why in this case userData is a pointer:

Student userData[2];
newStudent( userData );

If not, I will explain.

So to summarize: You need to hand over the ith element of userData to newStudent, where i is the index.

Now the cin problem.
I'm trying to find the words to explain what it is that you're doing wrong... but I fail to find them so I'll just show you how it should be done:

int iScore;
cin >> iScore;

userData.setScores( iScore );

This is, although working, not a 100% correct solution, but if you understood my first post correctly you should be able to spot what you still need to do to make it better.

Now the cin problem.
I'm trying to find the words to explain what it is that you're doing wrong... but I fail to find them so I'll just show you how it should be done:

cpp Syntax (Toggle Plain Text)

1.
int iScore;
2.
cin >> iScore;
3.

4.
userData.setScores( iScore );

int iScore; cin >> iScore; userData.setScores( iScore );

This is, although working, not a 100% correct solution, but if you understood my first post correctly you should be able to spot what you still need to do to make it better.

Wow, I feel silly. ><

Thanks a lot. I'm still just getting started in C++. We actually started pointers this week and I'm still trying to get a handle on them (In short, I don't really understand it :P)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.