如何使用数组获取输入,并将其存储在record.h类的主类中的变量中

How can I get input using an array and store in the variable in my main class from my record.h class?

本文关键字:record 变量 存储 获取 数组 何使用 输入      更新时间:2023-10-16

如何获取输入并将其存储到Record.h文件中定义的每个变量中?

我试图用一个数组来简化它,但我不知道如何做到这一点。我已经想了好几个小时了。我甚至不确定这是否可能。

这是我正在进行的任务,以了解

对于此活动,您将创建一个地址簿控制台程序那个1.在主程序中使用一个基本数组来保存多个Record类对象。2.Record类将由以下成员变量构造:Record number、名字、姓氏、年龄和电话号码。

3.Record类必须具有初始化成员变量的自定义构造函数。

4.Record类声明将与Record类实现分离,这些声明将放在.h和.cpp文件中分别地

5.程序应该有一个永久菜单,允许选择:

a。将信息输入到记录中,(这是我的问题)

b。显示所有记录中的所有信息,以及

c。退出程序。

6.程序至少应保存10条记录。

7.在提交此作业的.cpp和.h文件之前,使用0人输入、5人输入和10人输入测试输出。

Main.cpp

//
//  Main.cpp
//
//
//  Created by Jake Huckestein on 1/3/19.
//
#include <string>
#include <cstdlib>
#include <iostream>
#include "Record.h"
using namespace std;
int choice;
int main()
{
do {
cout << ("Menu") << endl;
cout << ("1.Input information into an record.") << endl;
cout << ("2.Display all information in all records.") << endl;
cout << ("3.Exit the program") << endl;
cout << ("Please enter your choice:") << endl;
cin >> choice;
switch (choice) {
case 1:
{
std::string fnameIN,lnameIN,idIN,telephoneIN,ageIN;
Record Item[10];
for (int i = 1; i < 11; i++)
{
cout <<("Employee ID:")<<endl;
cin >> Item;
}
break;
}
case 2:
for (int i = 1; i < 11; i++)
{
cout << Item[i].getemployeeID(idIN) << endl;
}
getchar();
break;
case 3:
return 0;
break;
}
} while (choice != 0);
}

记录.cpp

//
//  Record.cpp
//  Jake
//
//  Created by Jake Huckestein on 1/3/19.
//  Copyright © 2019 Jake Huckestein. All rights reserved.
//

#include "Record.h"
#include <string>
void Record::setemployeeID (string idIn)
{
employeeID = idIn;
}
void Record::setfirstName (string fnameIn)
{
firstName = fnameIn;
}
void Record::setlastName (string lnameIn)
{
lastName = lnameIn;
}
void Record::settelephone (string phoneIn)
{
telephone = phoneIn;
}
void Record::setage (string ageIn)
{
age = ageIn;
}
//get functions
string Record::getemployeeID()
{
return employeeID;
}
string Record::getfirstName()
{
return firstName;
}
string Record::getlastName()
{
return lastName;
}
string Record::gettelephone()
{
return telephone;
}
string Record::getage()
{
return age;
}

记录.h

//Author: Created by Jake Huckestein on 1/3/19.Date:
//FileName: Record.h
//Purpose: Creates an Record Class.
//Input: Data values input via set functions and assigned to member
vars.
//Output: Data values retrieved by get functions and returned to
caller.Exceptions:
#ifndef Record_h
#define Record_h
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Record
{
private:
string employeeID;
string firstName;
string lastName;
string telephone;
string age;
public:
Record()
{
}
Record(string idIn) :employeeID(idIn)
{
}
Record(string idIn, string fnameIn, string lnameIn)
:employeeID(idIn),firstName(fnameIn),lastName(lnameIn)
{
}
Record(string idIN, string fnameIN, string lnameIN, string telephoneIN)
:employeeID(idIN),firstName(fnameIN),
lastName(lnameIn),telephone(telephoneIN)
{
}
Record(string idIn, string fnameIn, string 
lnameIn,stringtelephoneIn, string ageIn)
:employeeID(idIn),firstName(fnameIn),
lastName(lnameIn),telephone(telephoneIn),age(ageIn)
{
if (telephoneIn.length() == 12
&& telephoneIn.at(3) == '-'
&& telephoneIn.at(7) == '-')
{
telephone = telephoneIn;
}
else
{
telephone = "000-000-0000";
}
}
void setemployeeID (string idIn);
void setfirstName (string fnameIn);
void setlastName (string lnameIn);
void settelephone (string phoneIn);
void setage (string ageIn);
string getemployeeID();
string getfirstName();
string getlastName();
string gettelephone();
string getage();
};
#endif /* Record_hpp */

我在main.cpp中尝试过的代码没有特定的顺序

Record Employee1("Ab123","Jake","Jones","205-612-5519","30");
Record Employee2("Ab124","Jakey","Jonesy","205-612-5518","31");
Record Employee3("Ab125","Jake","Jones","205-612-5519","30");
Record Employee4("Ab126","Jake","Jones","205-612-5519","30");
Record Employee5("Ab127","Jake","Jones","205-612-5519","30");
Record Employee6("Ab128","Jake","Jones","205-612-5519","30");
Record Employee7("Ab123","Jake","Jones","205-612-5519","30");
Record Employee8("Ab12310","Jake","Jones","205-612-5519","30");
Record Employee9("Ab1231","Jake","Jones","205-612-5519","30");
Record Employee10("Ab1232","Jake","Jones","205-612-5519","30");
Record Item[10];
for (int i = 0; i < 10; i++)
{
Item[i].setemployeeID("NA");
}
for (int i = 0; i < 10; i++)
{
cout << Item[i].getemployeeID() << endl;
cout << Employee1.getemployeeID() << endl;
cout << Employee1.getfirstName() << endl;
cout << Employee1.getlastName() << endl;
cout << Employee1.gettelephone() << endl;
cout << Employee1.getage() << endl;
cout << Employee2.getemployeeID() << endl;
cout << Employee2.getfirstName() << endl;
cout << Employee2.getlastName() << endl;
cout << Employee2.gettelephone() << endl;
cout << Employee2.getage() << endl;
cout << Employee3.getemployeeID() << endl;
cout << Employee3.getfirstName() << endl;
cout << Employee3.getlastName() << endl;
cout << Employee3.gettelephone() << endl;
cout << Employee3.getage() << endl;
cout << Employee4.getemployeeID() << endl;
cout << Employee4.getfirstName() << endl;
cout << Employee4.getlastName() << endl;
cout << Employee4.gettelephone() << endl;
cout << Employee4.getage() << endl;
cout << Employee5.getemployeeID() << endl;
cout << Employee5.getfirstName() << endl;
cout << Employee5.getlastName() << endl;
cout << Employee5.gettelephone() << endl;
cout << Employee5.getage() << endl;
cout << Employee6.getemployeeID() << endl;
cout << Employee6.getfirstName() << endl;
cout << Employee6.getlastName() << endl;
cout << Employee6.gettelephone() << endl;
cout << Employee6.getage() << endl;
cout << Employee7.getemployeeID() << endl;
cout << Employee7.getfirstName() << endl;
cout << Employee7.getlastName() << endl;
cout << Employee7.gettelephone() << endl;
cout << Employee7.getage() << endl;
cout << Employee8.getemployeeID() << endl;
cout << Employee8.getfirstName() << endl;
cout << Employee8.getlastName() << endl;
cout << Employee8.gettelephone() << endl;
cout << Employee8.getage() << endl;
cout << Employee9.getemployeeID() << endl;
cout << Employee9.getfirstName() << endl;
cout << Employee9.getlastName() << endl;
cout << Employee9.gettelephone() << endl;
cout << Employee9.getage() << endl;
cout << Employee10.getemployeeID() << endl;
cout << Employee10.getfirstName() << endl;
cout << Employee10.getlastName() << endl;
cout << Employee10.gettelephone() << endl;
cout << Employee10.getage() << endl;
for (int i = 1; i < 11; i++)
{
cout <<("Employee ID:")<<endl;
cin >> idIN;
cout <<("First Name:")<<endl;
cin >> fnameIN;
cout <<("Last Name:")<<endl;
cin >> lnameIN;
cout <<("Telephone #:")<<endl;
cin >> telephoneIN;
cout <<("Age:")<<endl;
cin >> ageIN;
}
}

就我理解的问题而言,你可以这样做:

Main.cpp

#include <iostream>
#include "Record.h"
#define RECORD_SIZE 10
using namespace std;
int main()
{
int choice;
Record items[RECORD_SIZE];
do {
cout << "Menun";
cout << "1.Input information into an record.n";
cout << "2.Display all information in all records.n";
cout << "3.Exit the programn";
cout << "Please enter your choice:n";
cin >> choice;
switch (choice) {
case 1: {
std::string fnameIN, lnameIN, idIN, telephoneIN, ageIN;
for (int i = 0; i < RECORD_SIZE; i++) {
cout << "Enter Employee ID:n";
cin >> idIN;
cout << "Enter Employee's First Name:n";
cin >> fnameIN;
cout << "Enter Employee's Last Name:'n";
cin >> lnameIN;
cout << "Enter Employee's Phone Number':n";
cin >> telephoneIN;
cout << "Enter Employee's Age:n";
cin >> ageIN;
items[i].setemployeeID(idIN);
items[i].setfirstName(fnameIN);
items[i].setlastName(lnameIN);
items[i].settelephone(telephoneIN);
items[i].setage(ageIN);
}
break;
}
case 2: {
for (int i = 0; i < RECORD_SIZE; i++) {
cout << "Employee " << (i + 1) << " details: n"
<< items[i].getemployeeID() << "n"
<< items[i].getfirstName() << "n"
<< items[i].getlastName() << "n"
<< items[i].gettelephone() << "n"
<< items[i].getage() << "n";
}
getchar();
break;
}
case 3: {
break;
}
default: {
cout << "Invalid Choice entered!";  
}
}
} while (choice != 3);
return 0;
}

建议:

1) 在Record类中,可以将getter函数设置为const。像这样:

string getfirstName() const;

2) 在这种情况下,您可以使用默认参数,而不是定义单独的构造函数。你可以这样做:

记录.h

#ifndef Record_h
#define Record_h
#include <string>
using namespace std;
class Record
{
private:
string employeeID;
string firstName;
string lastName;
string telephone;
string age;
public:
explicit Record(string idIn = "NA", string fnameIn = "NA", string lnameIn = "NA",
string telephoneIn = "NA", string ageIn = "NA") {
setemployeeID(idIn);
setfirstName(fnameIn);
setlastName(lnameIn);
settelephone(telephoneIn);
setage(ageIn);
}
void setemployeeID (string idIn);
void setfirstName (string fnameIn);
void setlastName (string lnameIn);
void settelephone (string phoneIn);
void setage (string ageIn);
string getemployeeID() const;
string getfirstName() const;
string getlastName() const;
string gettelephone() const;
string getage() const;
};
#endif /* Record_hpp */

3) 对于验证,请始终使用setter。

记录.cpp

#include "Record.h"
void Record::setemployeeID (string idIn) {
employeeID = idIn;
}
void Record::setfirstName (string fnameIn) {
firstName = fnameIn;
}
void Record::setlastName (string lnameIn) {
lastName = lnameIn;
}
void Record::settelephone (string phoneIn) {
// TODO: I would highly recommend you to do this using REGEX
if (phoneIn.length() == 12
&& phoneIn.at(3) == '-'
&& phoneIn.at(7) == '-') {
telephone = phoneIn;
} else {
telephone = "000-000-0000";
}
}
void Record::setage (string ageIn) {
age = ageIn;
}
//get functions
string Record::getemployeeID() const {
return employeeID;
}
string Record::getfirstName() const {
return firstName;
}
string Record::getlastName() const {
return lastName;
}
string Record::gettelephone() const {
return telephone;
}
string Record::getage() const {
return age;
}

4) 正如我在代码中提到的,您应该用涉及RegEx的替代方案来替换telephone相关的验证。

5) 对方法名称使用标准命名约定。