This is the Header file

#ifndef STUDENT_H
#define STUDENT_H

// class Student definition
class Student
{
public:
   Student( char *nPtr );
   ~Student();
   void displayGrades() const;
   Student addGrade( int );
   static int getNumStudents();

private:
   int *grades;
   char *name;
   int numGrades;
   int idNum;

   static int numStudents;

}; // end class Student

#endif // STUDENT_H

Here is my coding.. the only error is "strcpy_s( name, nPtr );" keeps saying it doesn't take 2 arguments.

#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <cstring>

#include "Student.h"

#include <new>
const int numStudents = 0;

// constructor
Student::Student(  char *nPtr )
{
   grades = new int[ 1 ];
   grades[ 0 ] = 0;
   name = new char[ strlen( nPtr ) + 1 ];
   strcpy_s( name, nPtr );
   numGrades = 0;
   ++numStudents;

   cout << "A student has been added\n";
} // end class Student constructor

// destructor
Student::~Student()
{
   cout << name << " has been deleted\n";
   delete grades;
   delete name;
   --numStudents;
} // end class Student destructor

// function displayGrades definition
void Student::displayGrades() const
{
   cout << "Here are the grades for " << name << endl;
   
   // output each grade
   for ( int i = 0; i < numGrades; i++ )
      cout << setw( 5 ) << grades[ i ];

   cout << endl << endl;
} // end function displayGrades

// function addGrade definition
Student Student::addGrade( int grade )
{
   int *temp = new int[ numGrades + 1 ];
   
   for ( int i = 0; i < numGrades; i++ )
      temp[ i ] = grades[ i ];

   temp[ numGrades ] = grade;
   grades = temp;
   ++numGrades;

   return *this;
} // end function addGrade

// function getNumStudents definition
int Student::getNumStudents()  
{
   return numStudents;
} // end function getNumStudents

Recommended Answers

All 3 Replies

errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);

You forgot the number of bytes you want to copy.

I thought that was shown from the parameters.. I changed the code to

strcpy( name, nPtr );

but it's saying the function or variable is unsafe

It is unsafe, that is why you want to use the _s variant. But the _s you need to use a little differently, because you need to hand over the size.

Remember that C-style strings are null-terminated, so strcpy will copy until it finds a null-char.

Anyway, I suggest that you change your code to use C++ strings instead of working with bare C style char arrays.

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.