searching thru a c++ class array

This is a discussion on searching thru a c++ class array within the C++ Programming forums, part of the General Programming Boards category; so i have gone thru my c++ class array and entered all the info needed..... class student { private: int ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    searching thru a c++ class array

    so i have gone thru my c++ class array and entered all the info needed.....

    class student
    {
    private:
    int ID;
    etc....
    };

    student stan[10];

    i have asked the user to enter his/her id # how do i check to verify that the # entered exists in the array stan[10]? (i know a for loop will start the process..)

    stan
    thanks

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    You could make a function similar to this:

    Code:
    bool CheckForDupes(int iTest) 
    { 
    bool RetVal=false; 
    for(int i=0;i<10;i++) //of course, i<? would be your array size 
    { 
    if(stan[i].GetID()==iTest) 
    { 
    RetVal=true; //duplicate found; 
    } 
    } 
    return(RetVal); 
    }
    then use this function like this:

    Code:
    int NewID, i=0; 
    while(i<10)
    { 
       cin>>NewID; 
       if(CheckForDupes(NewID)) 
       { 
         cout<<"Enter another ID. This one already exists."<<"\n"; 
        } 
        else 
        { 
           stan[i].SetID(NewID); 
           i++;
        }
    }

    This is basically what I PM'ed you except that I added the while loop here for a little more clarity. Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling with Array filled with a class
    By MarlonDean in forum C++ Programming
    Replies: 18
    Last Post: 06-27-2008, 10:53 AM
  2. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21