c++ - 如何使用数组获取输入并从我的 record.h 类存储在我的主类中的变量中?
问题描述
如何获取输入并将其存储到我在 Record.h 文件中定义的每个变量中?
我试图用一个数组来简化它,但我不知道该怎么做。我已经尝试了几个小时来解决这个问题。我不确定这是否可能。
这是我正在做的任务,以给予理解
对于本活动,您将创建一个地址簿控制台程序,该程序 1. 在主程序中使用一个基本数组来保存多个 Record 类对象。2. Record 类将由以下成员变量Record number、名字、姓氏、年龄和电话号码构成。
3.Record类必须有一个自定义的构造函数来初始化成员变量。
4.Record类声明与Record类实现分开,分别放在.h和.cpp文件中。
5.程序应该有一个永久菜单,允许选择:
一个。将信息输入记录,(这是我的问题)
湾。显示所有记录中的所有信息,以及
C。退出程序。
6. 程序至少应保存 10 条记录。
7.在提交此作业的 .cpp 和 .h 文件之前,以 0 人输入、5 人输入和 10 人输入测试输出。
主文件
//
// 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;
}
}
解决方案
据我了解这个问题,你可以这样做:
主文件
#include <iostream>
#include "Record.h"
#define RECORD_SIZE 10
using namespace std;
int main()
{
int choice;
Record items[RECORD_SIZE];
do {
cout << "Menu\n";
cout << "1.Input information into an record.\n";
cout << "2.Display all information in all records.\n";
cout << "3.Exit the program\n";
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)正如我在代码中提到的那样,您应该考虑telephone
用涉及 RegEx 的替代方法替换相关验证。
5) 对方法名称使用标准命名约定。
推荐阅读
- google-apps-script - “需要授权”和@OnlyCurrentDoc
- python - Pandas Dataframe 错误'StringArray 需要一系列字符串或 pandas.NA'
- python - Tkinter 绘制 jpeg 或 png
- python - 如何将两个熊猫数据框放在一起,一个数据框缺少日期
- javascript - 我的两个 jQuery 代码相互冲突
- tcl - 有没有办法在 TCL 中使用 IF CONDITION 期望语句?
- javascript - 向 Google 电子表格中的多个地址发送一封电子邮件
- python - 查找元组及其对应对的最大值
- r - 子集 - “。”的意思 在 R 中
- ajax - 使用 ajax 在 ASP.NET MVC 中发布数据时,数据为空