0

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

接下来,我希望将这个“newDude”复制到主地址 dudesList[0] 中声明的“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;

}
4

1 回答 1

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 < MAX; i++) //应该在将 Person 对象添加到数组 dudesList [indexToAdd] = newDude; 时迭代数组地址 //这是将newDude对象复制到数组indexToAdd++的地方;返回; }

于 2012-12-04T06:26:28.873 回答