将数据从.cpp文件传递到.h文件中的结构体中的c-string

Passing data into a c-string within a struct in a .h file from .cpp file

本文关键字:文件 c-string 结构体 数据 cpp      更新时间:2023-10-16

我在。h文件中定义了一个结构体。我必须将来自用户的信息从.cpp文件传递到结构体中的c字符串中。我不知道怎么做。有什么建议吗?h文件中的代码:

#ifndef EMPLOY_H_
#define EMPLOY_H_
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
struct employ
{
    const int nameLen = 30; //Length of name strings
    const int ssnLen = 11;  //Length of ssn string
    char firstName[nameLen];        //Employee first name
    char lastName[nameLen];     //Employee last name
    char midInit;           //Employee middle initial
    char SSN[ssnLen];           //Employee Social Security Number
    int hireDay,            //Day hired
    hireMonth,          //Month hired
    hireYear;           //Year hired
    double annSalary;       //Annual salary of employee
    long empNumber;         //Employee number
};

.cpp文件中的代码:

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include "employ.h"
using namespace std;
void sort(employee*dbaseArray, int numEmps);
void printEmps(employee*dbaseArray, int numEmps);
void printOne(employee person);
int main()
{
    int numEmps;
    int count = 1;
    cout << "How many employees are in the company? ";
    cin >> numEmps;
    if (numEmps < 1)
    {
        cout << "Sorry, that is an invalid number. Try Again: ";
        cin >> numEmps;
    }
    while (count <= numEmps)
    {
            cout << "What is employee's first name? ";
        cin.getline(firstName, nameLen);
    }
    return 0;
 }

在main中创建一个employ结构体的实例。你能改变employ的结构吗?

如果您可以更改,您可以更改char数组,您有三个std::string

您正在使用的sort算法也可能因std::algorithm中已经完成的内容而更改。

注意using namespace std;,事实上,不要在头文件中使用它。

int main()
{
    int numEmps;
    int count = 1;
    employ test; // This is your instance
    cout << "How many employees are in the company? ";
    cin >> numEmps;
    if (numEmps < 1)
    {
        cout << "Sorry, that is an invalid number. Try Again: ";
        cin >> numEmps;
    }
    while (count <= numEmps)
    {
        cout << "What is employee's first name? ";
        cin.getline(firstName, nameLen);
    }
    return 0;
 }

此外,如果您使用std::string,您将需要std::stringstreamstd::getline,这将为您节省一些关于n的麻烦。

例如:

cout << "How many employees are in the company? ";
cin >> numEmps;
if (numEmps < 1)
{
    cout << "Sorry, that is an invalid number. Try Again: ";
    cin >> numEmps;
}

   std::string input; 
   while (true) {
     std::cout << "How many employees are in the company? " << std::endl;
     std::getline(cin, input);
     std::stringstream myStream(input);
     if (myStream >> numEmps)
     {
       break;
     } else
     {
       std::cout << "Sorry, that is an invalid number." << std::endl;
     }
   }

对于你的第二个问题,你需要一个动态数组,我建议std::vector<employ>,你会这样使用它。

int main()
{
    int numEmps;
    int count = 1;
    cout << "How many employees are in the company? ";
    cin >> numEmps;
    if (numEmps < 1)
    {
        cout << "Sorry, that is an invalid number. Try Again: ";
        cin >> numEmps;
    }
    std::vector<employ> employees;
    employees.resize(numEmps);
    std::vector<employ>::iterator it;
    for(it = employees.begin(); it != employees.end(); ++it)
    {
        std::string name;
        cout << "What is employee's first name? ";
        std::getline(cin, name);
        name.copy(it->firstname, (it->nameLen)-1);
        it->firstname[it->nameLen] = ''; // Told you to change the struct char arrays for strings
    }
    return 0;
 }