2012-12-04 55 views
0

我正在研究一个项目,并且坚持我认为是范围和逻辑问题。我使用四个字符串变量,姓氏,名字,最喜欢的颜色和性别创建一个名为“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; 

} 
+1

tl; dr缩小范围,以便我们提供帮助。 –

+0

你想'addPersonToList'将人添加到最后?按照它的立场,它复制到每个元素 –

+5

使用一个向量并使'addPersonToList'使用'push_back'似乎更合乎逻辑。 – chris

回答

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; 
} 

你有几个选项。

什么可能是最容易做和concieve,是该指数传递到您的方法从那里你怎么称呼它,让你的方法是这样的:

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 
}; 

然后incriment在你addPersonToList方法

空隙PersonList :: addPersonToList(人newDude) { 对(INT I = 0; i < MAX;我应该迭代阵列地址,因为它将Person对象添加到数组中 dudesList [indexToAdd] = newDude; //这是newDude对象被复制到数组的地方 indexToAdd ++; return; }

+0

Sam我是,谢谢,谢谢,谢谢!它不仅对addPersonToList起作用,而且还能够编写一个非常简单的打印循环,以确保所有内容都可以正确输出到控制台,并且实际上为三个不同的人准确获得了三个不同的值,完全如我所料! 再次感谢您的帮助! –