c++ - Passing data into a c-string within a struct in a .h file from .cpp file -
i have struct defined in .h file. must pass in info user .cpp file c-string within struct. i'm not sure how. suggestions? code employ.h file:
#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 };
code .cpp file:
#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 in company? "; cin >> numemps; if (numemps < 1) { cout << "sorry, invalid number. try again: "; cin >> numemps; } while (count <= numemps) { cout << "what employee's first name? "; cin.getline(firstname, namelen); } return 0; }
any appreciated.
create instance of employ
struct in main. can make changes employ
structure?
if can make changes change char
arrays have in thre std::string
.
the sort
algorithm using changed done in std::algorithm
.
beware of using namespace std;
, in fact, don't use in header file.
int main() { int numemps; int count = 1; employ test; // instance cout << "how many employees in company? "; cin >> numemps; if (numemps < 1) { cout << "sorry, invalid number. try again: "; cin >> numemps; } while (count <= numemps) { cout << "what employee's first name? "; cin.getline(firstname, namelen); } return 0; }
also, if use std::string
you'll needing std::stringstream
, std::getline
, save headaches in future regarding \n
.
for example using:
cout << "how many employees in company? "; cin >> numemps; if (numemps < 1) { cout << "sorry, invalid number. try again: "; cin >> numemps; }
which be:
std::string input; while (true) { std::cout << "how many employees in company? " << std::endl; std::getline(cin, input); std::stringstream mystream(input); if (mystream >> numemps) { break; } else { std::cout << "sorry, invalid number." << std::endl; } }
for second question you'll needing dynamic array, suggest std::vector<employ>
, use this.
int main() { int numemps; int count = 1; cout << "how many employees in company? "; cin >> numemps; if (numemps < 1) { cout << "sorry, invalid number. try again: "; cin >> numemps; } std::vector<employ> employees; employees.resize(numemps); std::vector<employ>::iterator it; for(it = employees.begin(); != employees.end(); ++it) { std::string name; cout << "what employee's first name? "; std::getline(cin, name); name.copy(it->firstname, (it->namelen)-1); it->firstname[it->namelen] = '\0'; // told change struct char arrays strings } return 0; }
Comments
Post a Comment