943,714 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 284
  • C++ RSS
Apr 14th, 2009
0

Searching Array

Expand Post »
Hi,
I have been asked to create a small programme that asks a user how many students information they would like to enter and then asks the user to enter all their information storing it all into a struct.

The above is all good I know exactly know how to do this, but then we have to ask the user to seacrh for a student record using the student ID where they are then shown the record of that particular student.
Student ID will be an element in the struct. I am not sure exactly how to go about this!
Any help would be appreciated.
Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rudolph2004 is offline Offline
2 posts
since Apr 2009
Apr 14th, 2009
0

Re: Searching Array

What about a linked list?
Or alternatively, you could store all the class instances in an array with as many elements as the the user inputs, and to check which instance has the student's ID run a loop through all the indexes.
An example to make it all clearer:
c++ Syntax (Toggle Plain Text)
  1. ...
  2. struct StudentInfo
  3. {
  4. ...
  5. unsigned int ID;
  6. ...
  7. }
  8.  
  9. int main(void)
  10. {
  11. short int NumOfStudents;
  12. cout<<"Input information on how many students?\n";
  13. cin>>NumOfStudents;
  14. StudentInfo Students[NumOfStudents];
  15. short int Index;
  16. for(Index=0;Index<NumOfStudents;Index++)
  17. {
  18. ... // input students information
  19. }
  20.  
  21. unsigned int ID_ToLookFor;
  22. cout<<"Search for a student with what ID?\n";
  23. cin>>ID_ToLookFor;
  24. for(Index=0;Index<NumOfStudents;Index++)
  25. {
  26. if(Students[Index].ID==ID_ToLookFor) .. // do whatever you need to do here
  27. }
  28. ...
  29. }
Last edited by unbeatable0; Apr 14th, 2009 at 9:02 am.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++I need some Help Please?
Next Thread in C++ Forum Timeline: Strange (extra) data in buffer after ifstream::read()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC