Home c++ C++ strcpy error

C++ strcpy error [duplicate]

Author

Date

Category

There is an array of structures:

struct schoolboy
{
public:
  char firstName [30];
  char secondName [30];
  char addres [50];
  int schoolNumber;
  int schoolclass;
  schoolboy input (schoolboy * base);
  schoolboy recording (schoolboy * base, schoolboy * record, int number);
};

Here’s the implementation:

int main () {
  int number;
  setlocale (LC_ALL, "Rus");
  cout & lt; & lt; "Enter a number" & lt; & lt; endl;
  cin & gt; & gt; number;
  schoolboy base [28], record [28], a;
  a.input (base);
  a.recording (base, record, number);
  recordOut (record);
  system ("pause");
  return 0;
}

And here is the problematic function, the problem is in strcpy :

schoolboy schoolboy :: recording (schoolboy * base, schoolboy * record, int
number)
{
if (base- & gt; schoolNumber == number)
{
  if (base- & gt; schoolclass == 10 || base- & gt; schoolclass == 11)
  {
  cout & lt; & lt; "1" & lt; & lt; endl;
  for (int i = 0; i & lt; 2; i ++)
  {
    strcpy (record [i] .firstName, base [i] .firstName); // error here
    strcpy (record [i] .secondName, base [i] .secondName);
    strcpy (record [i] .addres, base [i] .addres);
  }
  }
  else
  {
    cout & lt; & lt; "Haven't 10 or 11 class" & lt; & lt; endl;
    return * base;
  }
 }
 else
 {
  cout & lt; & lt; "Not found" & lt; & lt; endl;
  return * base;
 }
 return * record;

How to fix the error? And could you give some advice on how to implement functions from structures in the “main”.

Error:

error C4996: 'strcpy': This function or variable may be unsafe.
Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Answer 1, authority 100%

Replace all strcpy with strcpy_s.


Answer 2

If you don’t want to deal with studio troubles, there is another way. In the project settings, specify this as a predefined macro:

# define _CRT_SECURE_NO_WARNINGS

Or you can add this to the top of the file.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions