0
我正在研究這個控制學生信息的C++程序。我已經能夠添加和顯示學生的信息。我需要通過他的ID在特定學生的數組中搜索數組,但是爲了搜索ID,似乎將類型類的數組賦值給int
變量時出現問題?搜索類型數組的數組
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
class student{
private:
int id, age;
std::string fname, lname;
string cob;
char s;
public:
student()
{
id=age=0;
fname=lname=cob="";
}
void setid(int);
void setage(int);
void setfname(string);
void setlname(string);
void setsex(char);
void setcob(string);
int getid();
int getage();
string getfname();
string getlname();
string getcob();
char getsex();
};
void student::setfname(string n)
{ fname=n;}
void student::setlname(string n)
{ lname=n;}
void student::setcob(string n)
{ cob=n;}
void student::setid(int n)
{ id=n;}
void student::setage(int n)
{ age=n;}
void student::setsex(char n)
{ s=n;}
string student::getfname()
{ return fname;}
string student::getlname()
{ return lname;}
string student::getcob()
{ return cob;}
int student::getid()
{ return id;}
int student::getage()
{ return age;}
char student::getsex()
{ return s;}
std::array<student, 100> studentlist;
void addstd()
{
int id, age;
string fname, lname;
string cob;
char s;
for(int i=0;i<3;i++)
{
cout<<"Enter ID Number"<<endl;
cin>>id;
studentlist[i].setid(id);
cout<<"Enter First Name"<<endl;
cin>>fname;
studentlist[i].setfname(fname);
cout<<"Enter last Name"<<endl;
cin>>lname;
studentlist[i].setlname(lname);
cout<<"Enter age"<<endl;
cin>>age;
studentlist[i].setage(age);
cout<<"Enter student's Sex(F or M) "<<endl;
cin>>s;
studentlist[i].setsex(s);
cout<<"Enter Country of birth"<<endl;
cin>>cob;
studentlist[i].setcob(cob);
}
}
void displayinfo()
{
for(int i=0;i<3;i++)
{
cout<<"Student ID: "<<studentlist[i].getid()<<endl;
cout<<"First Name: "<<studentlist[i].getfname()<<endl;
cout<<"Last Name: "<<studentlist[i].getlname()<<endl;
cout<<"Student Age: "<<studentlist[i].getage()<<endl;
cout<<"Student Gender: "<<studentlist[i].getsex()<<endl;
cout<<"Student Birth City: "<<studentlist[i].getcob()<<endl;
cout<< "\n";
}
}
void searchstd()
{
int num;
cout<<"Enter ID of Student "<<endl;
cin>>num;
int temp;
for(int i=0;i<3;i++)
{
studentlist[i]=temp; // here is the problem!!
if(temp==num)
{
}
}
}
/*std::array<student, 100> studentlist;*/
void main()
{
/*
student s;
student std[100];
*/
student s;
int choice;
do{
cout << "-----------Menu------------" << endl;
cout << " 1. Add a student " << endl;
cout << " 2. Search for student by ID " << endl;
cout << " 3. Display all students information " << endl;
cout << " 4. Remove a students " << endl;
cout << " 5. Display students aged between 34 - 50 " << endl;
cout << " 6. Modify a student's information " << endl;
cout << " 7. Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5,6,7 " << endl;
cin>>choice;
switch(choice) {
case 1:
addstd();
break;
case 2:
searchstd();
break;
case 3:
displayinfo();
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
exit(0);
break;
default:
cout << "Please enter 1, 2, 3, 4, 5, 6 or 7 : " << endl << endl;
}
}while(choice!=8);
}
我認爲你應該做studentlist [I] .ID =溫度 –
沒有@tmp你錯了。您不能訪問班級以外的「私人」字段。正確的語法是:'studenlist [i] .setid(temp);' – 2014-03-19 17:15:03
謝謝@fiskerXO更正:)...我只是很快看了看,認爲它是ctype結構體。 –