Lists: output, fill, list search - C++
Hello, can someone tell me and show me how to correctly fill in, output a list that is based on a structure with 5 elements of different types, for example:
struct my {
char name[20];
float amount;
int number;
};
And how to search for list items.
void search(float point){
Students *tmp=head;
while (tmp!=0){
//for (int a=0;a<3;a++){
if (tmp->average_point>point){
cout << "Match";
cout << tmp->average_point;
search(point);
break;
}
else{
tmp=tmp->Next;
}
}
}
With this search, only one element is output, and how to make all the elements that fall into the condition be output?
2 answers
Try this. The break statement, after the first finding, interrupted the loop for you. Executed only when the if did not match, not correctly, should be executed anyway.
else{
tmp=tmp->Next;
}
And I don't understand why you call the function search(point);
{[4 again]}
Here I redid what came out for me
void search(float point){
Students *tmp=head;
while (tmp!=0){
//for (int a=0;a<3;a++){
if (tmp->average_point>point){
cout << "Match";
cout << tmp->average_point;
}
tmp=tmp->Next;
}
}
Here's an example, though I use the person class here instead of a structure, but as a list (array) I have a Vector. Well, you can safely use other types of lists(sheets, queue), it all depends on the situation.
//Запись в файл
void RabotaAccount::wrt()
{
vector <person>::iterator iter1;
ofstream file;
file.open("GROUP.DAT", ios::trunc);
int n = 0;
iter1 = persVector.begin();
while(iter1 != persVector.end())
{
cout << (*iter1);
file.write( reinterpret_cast<char*>(&(*iter1)), sizeof((*iter1)) );
*iter1++;
n++;
}
cout << n <<endl;
persVector.clear();
file.close();
}
//вывод данных в память
void RabotaAccount::readPolz()
{
person pers;
fstream file;
persVector.clear();
file.open("GROUP.DAT", ios::app | ios::out |
ios::in);
file.seekg(0); //reset to start of file
//read first person
file.read( reinterpret_cast<char*>(&pers), sizeof(pers) );
while( !file.eof() ) //quit on EOF
{
//display person
persVector.push_back(pers);
//read another person
file.read( reinterpret_cast<char*>(&pers), sizeof(pers) );
}
file.close();
}