Searching Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 2
Reputation: rudolph2004 is an unknown quantity at this point 
Solved Threads: 0
rudolph2004 rudolph2004 is offline Offline
Newbie Poster

Searching Array

 
0
  #1
Apr 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Searching Array

 
0
  #2
Apr 14th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC