打印对象数组的每个元素时出现问题

时间:2012-12-04 06:14:53

标签: c++ arrays class object loops

我正在研究一个项目,我坚持认为是范围和逻辑问题。我正在创建一个名为“Person”的类,它有四个字符串变量,姓氏,名字,喜欢的颜色和性别。我正在创建一个名为'PersonList'的第二个类,其中一个变量是'Person'对象的数组。我设置了一个名为MAX的const int来确定数组的大小(目前为3)。最后,我想将一个'Person'对象的字符串变量输入到main中声明的'newDude'对象中。例如,当'readNewPerson'函数运行时,我输入Doe,John,Blue,Male。这一步工作!!

接下来,我希望将这个'newDude'复制到在地址dudesList [0]和'readNewPerson'函数中声明的'dudesList'对象中再次运行。如果一切正常,我认为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;

}

1 个答案:

答案 0 :(得分:1)

好吧,似乎出了什么问题,就是你要将同一个人添加到列表中3次。当你致电addPersonToList

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::addPersonToList (Person newDude, int index)
{
    dudesList [index] = newDude; //this is where the newDude object is copied to the array
}

然后在你的switch语句中,像这样调用它

case 1:
    for (int i = 0; i < MAX; i++)
        {
        newDude.readNewPerson (); //function call to enter one person's info
        List.addPersonToList (newDude, i); //function call to add newDude to dudesList array
        }


现在可能是“正确”的方法,就是跟踪你在PersonList类中添加的最后一个索引

class PersonList
{
    private:
        Person dudesList [MAX];
        int indexToAdd = 0;  // your new index
    public:
        void addPersonToList (Person newDude); //function declaration to add my newDude to the dudesList array
        void printList (); //function declaration to print the entire array   
};

然后在addPersonToList方法

中加入

void PersonList :: addPersonToList(Person newDude) {     for(int i = 0; i&lt; MAX; i ++)//应该迭代数组地址,因为它将Person对象添加到数组中     dudesList [indexToAdd] = newDude; //这是将newDude对象复制到数组的位置     indexToAdd ++;     返回; }