我正在按照上一个问题的指导再次尝试。希望这次我做对了。我真的希望这个问题也不要太宽泛,如果是这样,我很抱歉。
如何创建具有"字符串名称"和"整数年龄"的 4 人列表(作为数组)?我做错了什么?
class person
{
public:
string name;
int age;
void SetInfo(char* _name, int _age)
{
name = _name;
age = _age;
};
};
我做错了什么?
int main()//start of program
{
person mylist[4];//List of people
mylist[0].setinfo("Calle", 22)
mylist[1].setinfo("Calle", 23)
mylist[2].setinfo("Calle", 24)
mylist[3].setinfo("Calle", 25)
int index = Linearsearch(mylist, ...)
if (index == -1)
cout << "person not found!";
else
cout << "the person you searched for" << mylist[index].name << index;
cin.get();
return 0;
system("pause");
}
这适用于行搜索和气泡排序吗?
这是行搜索的代码:(它在 int main 中,所以我不妨在这里添加它。随意忽略它)
int linesearch(Person p[], int n, int a)
{
for (int i = 0; i < 10; i++)
if (person[i].person age == key)
return i;
};
您将成员函数命名为 SetInfo,但主要在调用中将其命名为 setinfo
此外,您忘记在类定义中放置右大括号。
函数行搜索必须定义为
int linesearch( const person p[], int n, int key)
{
int i = 0;
while ( i < n && p[i].age != key ) i++;
return ( i == n ? -1; i );
};
并调用主,例如
int index = linearsearch(mylist, 4, 23 );
同样,在您的代码中,此函数的名称不要在函数调用和函数定义中重合。
Funcion SetInfo必须声明为
void SetInfo( const char* _name, int _age);
如果使用字符串文本作为其参数。
至于泡沫排序,我什么也说不出来,因为你既没有显示它的定义,也没有显示它的使用。
你能在程序的所有地方输入相同的名称吗?!!!