微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

请帮助:将 strcpy_s 与 char 数组一起使用时的数据存储错误和其他杂项错误

如何解决请帮助:将 strcpy_s 与 char 数组一起使用时的数据存储错误和其他杂项错误

我在 get_company_info() 函数中分配字符数组时遇到问题,并且在 get_employees 函数中出现段错误。我在函数中玩过 couts 并且无法弄清楚我做错了什么,只有垃圾打印了名称数组的 int main() ;这是必须打印数据的地方(不要问为什么),但是在函数中打印时,由于某种原因,我在 strncpy_s 之后放置了公司名称,但无法将其复制到指针数据库中出于某种原因

我真的很需要帮助(在接下来的几个小时内),但不知道该怎么做,感谢您的反馈。

... 
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

//**********************************************************************
//*                         Symbolic Constants                         *
//**********************************************************************
#define COMPANY_ALLOC_ERR            1       //
#define EMPLOYEE_ALLOC_ERR           3       //
#define MAX_NAME_LENGTH              80      // Maximum length of the company name
#define NAME_ALLOC_ERR               2       //

//**********************************************************************
//*                         Program Structures                         *
//**********************************************************************
// Database record of company information
struct company_info
{
    const char  *p_company_name;   //
    float bonus_year;        //
    int   years_worked,//
    employee_quantity; //
};

//**********************************************************************
//*                           Program Classes                          *
//**********************************************************************
// Database record of company employee bonuses
class employee_records
{
    int   id,//
    service_years,//
    year_hired;    //
    float bonus;         //
public:
    // Set the data members
    void set_id            (int   i){id = i;               }
    void set_service_years (int   s){service_years = s;    }
    void set_year_hired    (int   y){year_hired = y;       }
    void set_bonus         (float b){bonus = b;}

    // Get the data members
    int   get_id           ()       {return id;            }
    int   get_service_years()       {return service_years; }
    int   get_year_hired   ()       {return year_hired;    }
    float get_bonus        ()       {return bonus;         }

    // Destructor,delete the employee bonus records database
    ~employee_records();
};

//**********************************************************************
//*      Delete the bonus record and print the destructor message      *
//**********************************************************************
employee_records :: ~employee_records()
{
    cout << "Destructor executing ...";
}

//**********************************************************************
//*                        Function Prototypes                         *
//**********************************************************************
void print_instructions              ();
// Print the program instructions
struct company_info *get_company_info();
//
employee_records get_employees       (struct company_info new_company_info);
//
void print_employees                 (employee_records *p_employee_records,int employee_quantity,const char *p_order_sort);
//
void sort_database                   (int employee_quantity,employee_records *p_employee_start);
//
void fatal_error                     (int error_number,const char *p_company_name,const char *p_function_name);
//
//**********************************************************************
//*                           Main Function                            *
//**********************************************************************
int main()
{
    struct company_info     *p_company_info;
    employee_records *p_employee_records;

    // Print the program heading and instructions
    print_instructions   ();

    // Get and print the company information
    p_company_info = get_company_info ();
    cout << "\n\nCompany name:        " << p_company_info->p_company_name;
    cout <<   "\nYear of the bonuses: " << p_company_info->years_worked;
    cout <<   "\nNumber of employees: " << p_company_info->employee_quantity;
    cout <<   "\nBonus per year:      " << p_company_info->bonus_year;

    // Get and print the unsorted employee bonus records database
    *p_employee_records = get_employees(*p_company_info);
    print_employees                    (p_employee_records,p_company_info->employee_quantity,"IN UNSORTED ORDER:");

    // Get and print the sorted employee bonus records database
    sort_database                     (p_company_info->employee_quantity,p_employee_records);
    print_employees                   (p_employee_records,"SORTED BY YEAR HIRED:");

    // Release memory allocated for database of employee bonus records
    delete []p_employee_records;
    delete []p_company_info->p_company_name;
    delete   p_company_info;

    // Say goodbye and terminate the program
    cout <<       "\n\n\nThanks for processing employee bonuses today ;)";
    cout << "\n\n\n\n\n\n";
    return 0;
}

//**********************************************************************
//*                  Print the program instructions                    *
//**********************************************************************
void print_instructions()
{
    cout << "\n       ========================================================";
    cout << "\nThis program asks for information about your company and";
    cout << "\nabout each employee. It then calculates the bonus amount";
    cout << "\Nowed each employee based on the number of service years.";
    return;
}

//**********************************************************************
//*                                     *
//**********************************************************************
struct company_info *get_company_info()
{
    char   company_name[MAX_NAME_LENGTH + 1]; //
    struct company_info *p_company_info;      //

    // Allocate the company information
    try
    {
        p_company_info = new company_info;
    }
    catch (bad_alloc xa)
    {
        fatal_error(COMPANY_ALLOC_ERR,p_company_info->p_company_name,"get_company_info");
    }

    // Allocate and get the company name

    try
    {
        p_company_info->p_company_name = new char [strlen(company_name)+1];
    }
    catch (bad_alloc xa)
    {
        fatal_error(NAME_ALLOC_ERR,"get_company_info");
    }
    cout << "\nEnter the name of your company here (no spaces): ";
    cin  >> company_name;
    strcpy_s(company_name,strlen(p_company_info->p_company_name) + 1,p_company_info->p_company_name);


    cout << company_name;
    cout << p_company_info->p_company_name;

    // Get the number of employees
    do
    {
        cout << "\nEnter your number of employees (1 or more): ";
        cin  >> p_company_info->employee_quantity;
    } while(p_company_info->employee_quantity < 1);

    // Get the year of the bonuses
    cout <<      "Enter the year in which the bonuses are given (YYYY): ";
    cin  >> p_company_info->years_worked;

    // Get the yearly bonus
    cout <<      "Give the yearly bonus amount per employee (in dollars): ";
    cin  >> p_company_info->bonus_year;

    //Return pointer to the structure
    return p_company_info;
}

//**********************************************************************
//*             *
//**********************************************************************
employee_records get_employees(struct company_info new_company_info)
{
    employee_records *p_start_employees  = NULL,*p_moving_employees = NULL;
    int              id                  = 1,service_years;

    // Allocate the employee bonus database
    try
    {
        p_start_employees = new employee_records;
    }
    catch(bad_alloc xa)
    {
        fatal_error(EMPLOYEE_ALLOC_ERR,new_company_info.p_company_name,"get_employees");
    }
    p_moving_employees = p_start_employees;

    // Get every employee's years of service
    do
    {
        // Loop processing valid number of service years
        do
        {
            cout << "\n\nEnter the number of service years of employee # " << id
                 <<      ".";
            cout <<   "\nEnter 0 (zero) if this employee does not exist:";
            cin  >> service_years;

            if (service_years < 0)
            {
                cout << "\n   The service years must be 0 or greater.";
                cout << "\n   Please reenter the number of service years.";
            }
        }while(service_years < 0);


        //
        if (service_years > 0)
        {
            p_moving_employees->set_id           (id);
            p_moving_employees->set_service_years(service_years);
            p_moving_employees->set_year_hired   ((int)(new_company_info.bonus_year - service_years));
            p_moving_employees->set_bonus        ((float)(new_company_info.years_worked * service_years));
            p_moving_employees++;
        }
        id++;

    }while((p_moving_employees - p_start_employees) < new_company_info.employee_quantity);

    // Return the pointer to the database of employee records
    return *p_start_employees;
}

//**********************************************************************
//*                                     *
//**********************************************************************
void print_employees(employee_records *p_employee_records,const char *p_order_sort)
{
    employee_records *p_employee = NULL; // Points to the every employee record

    cout << "\n\nHere is the employee database," << p_order_sort
         << ":";
    cout << "\n====================================================";
    cout << "\nEmployee Id   Service Years   Year Hired   Bonus Amt";
    cout << "\n-----------   -------------   ----------   ---------";

    for(p_employee = p_employee_records;
        (int)(p_employee - p_employee_records) < (employee_quantity -1); p_employee++)
    {
        cout << "\n     "     << p_employee->get_id           ()
             <<   "     ";
        cout <<   "         " << p_employee->get_service_years()
             <<   "      ";
        cout <<   "      "    << p_employee->get_year_hired   ()
             <<   "    ";
        cout <<   "    $"     << p_employee->get_bonus        ();
    }
    return;
}

//**********************************************************************
//*                                     *
//**********************************************************************
void sort_database(int employee_quantity,employee_records *p_employee_start)
{
    employee_records *p_employee_moving  = NULL,//
    temporary_employees;//
    int              sort_counter;               //

    for(sort_counter = 1; (sort_counter < employee_quantity); sort_counter++)
    {
        for(p_employee_moving = p_employee_start;
            (int)(p_employee_moving - p_employee_start) <
            (employee_quantity - sort_counter);)
        {
            if(p_employee_moving->get_service_years       () <
               (p_employee_moving + 1) -> get_service_years() )
            {
                temporary_employees      = *p_employee_moving;
                *p_employee_moving       = *(p_employee_moving + 1);
                *(p_employee_moving + 1) = temporary_employees;
            }
        }

    }

    return;
}

//**********************************************************************
//*                                     *
//**********************************************************************
void fatal_error(int error_number,const char *p_function_name)
{
    cout <<"\n\nError # "                                   << error_number
         <<     " occurred in the "                         << p_function_name
         <<     " function,unable to allocate memory for " << p_company_name;
    cout << "\nThe program is aborting...";
    exit(error_number);
    return;
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。