私はプロジェクトに取り組んでいて、スコープとロジックの問題だと思うことに固執しています。家系の名前、名、好きな色、性別の4つの文字列変数を持つ「Person」というクラスを作成しています。'Person'オブジェクトの配列である1つの変数を使用して、'PersonList'という2番目のクラスを作成しています。配列のサイズ(現在は3)を決定するMAXという定数を設定しました。最終的には、メインで宣言されている「newDude」オブジェクトに「Person」オブジェクトの文字列変数を入力したいと思います。たとえば、「readNewPerson」関数を実行すると、Doe、John、Blue、Maleと入力します。このステップは機能します!!
次に、この「newDude」をメインのアドレスdudesList[0]で宣言された「dudesList」オブジェクトにコピーして「readNewPerson」関数を再度実行したいと思います。すべてが正しく機能する場合は、Smith、Jane、Pink、FemaleをdudesList [1]にコピーし、Johnson、Mike、Green、MaleをdudesList[2]にコピーする必要があると考えています。最後に、3つのオブジェクトすべてをコンソールに出力したいと思います。
変数が適切に反復されていない「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;
}