将数据从.cpp文件传递到.h文件中的结构体中的c-string
Passing data into a c-string within a struct in a .h file from .cpp file
我在。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::stringstream
和std::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;
}
相关文章:
- 'string'文件在带有C++文件的iOS插件上找不到
- C++将文件行分离为String和Int
- pip install uwsgi 失败,出现 /usr/local/include/string/string.h:7:10:致命错误:找不到'sstream'文件?
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法将文件的内容读入 std::string
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- Qt UTF-8 文件到 std::string 添加额外的字符
- 将C 数组写成文件,避免创建std :: String
- 我是否也应该包括 string.h,即使它存在于头文件中
- 如何使用矢量数据使用 rapidjson 库创建 json 文件<string>?
- 使用 std::find 查找从二进制文件中读取的字符并转换为 std::vector 中的 std::string<string> 会产生这种不可预测的行为?
- YAML::LoadFile(std::string const&) 找不到文件 [ROS 中的 yaml-cpp]
- 通过读取文件创建映射<字符串,矢量>时如何管理内存<string>
- 将文件的二进制内容推送到 std::string 以用于 Google 的 Snappy
- 如何在C 中的文件中直接读取std :: String中的字符串
- 比较输入的C弦与文件读取c-string
- 使用 string::find 来解析文件
- 使用 std::write 和 std::read C++ 读取包含 std::string 到.dat文件的结构
- Ubuntu - 没有这样的文件或目录 #include <string>
- C++ 将矢量的排序内容写入<string>文件