【发布时间】:2012-12-04 06:14:53
【问题描述】:
我正在做一个项目,但我被我认为的范围和逻辑问题所困扰。我正在创建一个名为“Person”的类,其中包含四个字符串变量、姓氏、名字、最喜欢的颜色和性别。我正在创建第二个名为“PersonList”的类,其中一个变量是“Person”对象的数组。我设置了一个名为 MAX 的 const int 来确定数组的大小(当前为 3)。最终,我想将“Person”对象的字符串变量输入到 main 中声明的“newDude”对象中。例如,当“readNewPerson”函数运行时,我输入 Doe、John、Blue、Male。这一步有效!!
接下来,我希望将这个 'newDude' 复制到地址 dudesList[0] 的 main 中声明的 'dudesList' 对象中,并再次运行 'readNewPerson' 函数。如果一切正常,我认为应该将 Smith、Jane、Pink、Female 复制到 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似乎更合乎逻辑。 -
Karthik,是的。我希望每个 newDude 对象都被复制到下一个数组索引。所以,我输入的第一个人(Doe,John,Blue,Male)进入 dudesList[0],下一个人(Smith,Jane,Pink,Female)进入 dudesList[1],等等。单个 newDude 对象实际上是临时的并且仅用于收集个人数据。 dudesList 数组是我希望存储所有对象的位置。
-
克里斯,我的课没有涉及矢量。这是老师想要的令人难以置信的简化版本。归根结底,这应该是一个具有复杂字符串操作的成熟联系人列表。我只是想通过使用一个单词的字符串和一个 3 元素数组来学习一些非常基本的概念。
标签: c++ arrays class object loops