我正在研究一个项目,并且坚持我认为是范围和逻辑问题。我使用四个字符串变量,姓氏,名字,最喜欢的颜色和性别创建一个名为“Person”的类。我创建了一个名为'PersonList'的第二个类,其中一个变量是'Person'对象的数组。我设置了一个名为MAX的const int,它决定了数组的大小(当前为3)。最终,我想将一个'Person'对象的字符串变量输入到main中声明的'newDude'对象中。例如,当'readNewPerson'函数运行时,我输入Doe,John,Blue,Male。这一步工作!在打印对象数组中的每个元素时遇到问题
接下来,我想将这个'newDude'复制到dudesList [0]和readNewPerson函数中重新运行的dudesList对象中。如果一切正常,我认为史密斯,简,粉红色,女性应该被复制到dudesList [1],Johnson,Mike,Green,Male应该被复制到dudesList [2]。最后,我想打印所有三个对象到控制台。
我在'addPersonToList'和'printList'函数中'for'循环的地方出错,我的变量没有正确迭代。我的猜测是,因为我在函数内部声明了计数器'i',所以每当创建'newDude'时它就会死亡并重置为零。如果我对此是正确的,那么宣布柜台的最佳位置在哪里?我应该在什么时候迭代它?
我真的很感谢任何人可能会提供的反馈,我当然不希望任何人为我完成任务。至少在概念上,我对这个非常简单的任务感到非常沮丧,因此我可以在这一点上采取一些正确的方向。
这是到目前为止我的程序:
// contact list with one-word strings only
#include <iostream>
using namespace std;
const int MAX = 3;
class Person
{
private:
string dudesLName;
string dudesFName;
string dudesColor;
string dudesGender;
public:
void readNewPerson(); // function declaration to ask for info
void printPerson(); //function declaration to display info to console
};
class PersonList
{
private:
Person dudesList [MAX];
public:
void addPersonToList (Person newDude); //function declaration to add my newDude to the dudesList array
void printList(); //function declaration to print the entire array
};
//the main function is a simple switch-case asking for user choices
int main (void)
{
Person newDude; //one object contains 4 simple string variables
PersonList List; //one object contains an array of [MAX] dudes
int userChoice; //integer for the user's choice in switch-case
cout << "~~Welcome to Stephen's Contact List~~" << endl;
cout << "Please enter your choice:" << endl;
cout << " 1 - enter " << MAX << " new people" << endl;
cout << " 2 - print the contact list" << endl;
cout << " 3 - retrieve by last name" << endl;
cout << " 4 - retrieve by address" << endl;
cout << " 5 - retrieve by gender" << endl;
cin >> userChoice;
switch (userChoice)
{
case 1:
for (int i = 0; i < MAX; i++)
{
newDude.readNewPerson(); //function call to enter one person's info
List.addPersonToList (newDude); //function call to add newDude to dudesList array
}
break;
case 2:
cout << "2 doesn't work yet" << endl;
List.printList(); //function call to print entire list
break;
case 3:
cout << "3 doesn't work yet" << endl;
break;
case 4:
cout << "4 doesn't work yet" << endl;
break;
case 5:
cout << "5 doesn't work yet" << endl;
break;
}
cout << "thanks dude!!" << endl;
return 0;
}
// function definitions
void Person::readNewPerson()
{
cout << "enter a dude's last name please" << endl;
cin >> dudesLName;
cout << "enter a dude's first name please" << endl;
cin >> dudesFName;
cout << "enter a dude's favorite color please" << endl;
cout << "for test purposes, just enter one word" << endl;
cin >> dudesColor;
cout << "enter a dude's gender please" << endl;
cout << "male or female is fine, so is dude or dudette" << endl;
cin >> dudesGender;
return;
}
void Person::printPerson()
{
cout << "dude's name is " << dudesLName << ", " << dudesFName << endl;
cout << "his (her?) favorite color is: " << endl;
cout << dudesColor << endl;
cout << "and his (her?) gender is: " << endl;
cout << dudesGender << endl << endl;
return;
}
void PersonList::addPersonToList (Person newDude)
{
for (int i = 0; i < MAX; i++) //supposed to iterate the array address as it adds Person objects to the array
dudesList [i] = newDude; //this is where the newDude object is copied to the array
return;
}
void PersonList::printList()
{
for (int i = 0; i < MAX; i++)
dudesList [i].printPerson();
return;
}
tl; dr缩小范围,以便我们提供帮助。 –
你想'addPersonToList'将人添加到最后?按照它的立场,它复制到每个元素 –
使用一个向量并使'addPersonToList'使用'push_back'似乎更合乎逻辑。 – chris