hey ..

please i need tutorial 4 vector .. because i'm tring to convert this code from array to vector .. but i failed .. so i think my way is wrong .. :(

#include<iostream>
using namespace std;
#include<string>
#include<vector>

class student_recored{
vector<int> ex;
       string name;
       long ID;

public:

    void set_name(string n)
    {
               name=n;
    }
    void set_id(long id)
    {
               ID=id;
    }
    string getname()
    {
      return name;
    }
    long getid()
    {
      return ID;
    }

void SearchId(vector<int> &ex, int num , long studentid){
        for ( int j=0; j < num; j++ ){
            if ( studentid == ex[j].getid() ){
                cout<< "student is found .. student name is : "<< ex[j].getname()<< endl;
                break;}
            else
                cout<< "student is not found " << endl;}
    }
};

int main()
{
	vector<int> ex;
    int num=0,choose,i;
    string n;
    long id;

cout<<"      *******************\n";
cout<<".... Welcome To Student Program ...\n";
cout<<"Choose one of the following :- \n";
cout<<"1- Add students.\n";
cout<<"2- search for student .\n";
cout<<"3- Exit ...\n";
cout<<"      ********************\n";
cout<<"Please Enter your choose : ";
cin>>choose;
while ( choose != 3 ){  
switch (choose)
{
case 1 :
    cout<< "\nplease enter how many students do you want to add : ";
    cin>>num;
   for (i=0; i<ex.size(); i++)
       {
    cout<<"\nplease enter the name for student ["<<i<<"] : ";
    cin>>n;
    cout<<"\nplease enter the id for student ["<<i<<"] : ";
    cin>>id;
    ex[i].set_name(n);
    ex[i].set_id(id);
       }
    break;
case 2 :
    long studentid;
    cout<<"please enter id number to search on it :  ";
    cin>>studentid;
    ex[i].SearchId(ex,num ,studentid);    
break;
case 3 :
      cout<<"Exist ..\n";
    break;

default : cout<<"wrong choose number\n";
    break;
}
cout<<"\nEnter your choose : ";
cin>>choose;
}
return 0;
}

thanx .. :)

I think I see what you are trying to do.

You have a list of students, where each student has a name and an ID.

#include <iostream>
#include <vector>
using namespace std;

// This is our student, who has a name and an ID
struct student_t {
  string name;
  long ID;
  // This is a constructor to help us create a new student
  student_t( string aName, long aID ):
    name( aName ), ID( aID ) { }
  };

int main() {
  // This is our list of students. There can be as many
  // or as few students as you like.
  // Initially, there aren't any students.
  vector<student_t> students;
  // (This takes the place of an array
  //  like "student_t students[ 100 ];")

  // Let's fill our array with students
  string s;
  long id;
  while (s != 'n')
  {
    cout << "Please enter a student name: ";
    getline( cin, s );
    cout << "What is " << s << "'s ID? ";
    cin >> id;

    // Now let's create a new student with the information
    // we have and append it to the list of students.
    students.push_back( student_t( s, id );

    // ignore the newline from the last cin. This is necessary
    // because I mixed >> with getline(). The next time I get
    // a student name I want the name, not what was left over
    // on the end of the line when I asked for the ID...
    getline( cin, s );

    cout << "Another (y/n)? ";
    getline( cin, s );
  };

  // Now, let's tell the user what he already knows...
  // Notice how I can get the length of the
  // list with "students.size()" and I can access
  // individual students as if the list were an array.
  cout << "The students are:\n";
  for (int i = 0; i < students.size(); i++)
    cout << "ID=" << students[ i ].ID << ", "
         << students[ i ].name << endl;

  return EXIT_SUCCESS;
  }

The problem you are having with your code above is that it mixes data types. The type of a thing is very important. You have several types of things:
- an ID
- a name
- an individual student (which has an ID and a name)
- a list of students

So, to find a particular student, you'll need a function that works on an list of students, not a student that works on a list. Make sense? int find_student( vector<student_t> student_list, long ID_to_find ) This function should search through the student_list until it finds a student with the ID_to_find, then return the position in the list of the student (or -1 if the student isn't found).

Hope this helps.

[EDIT] Before I forget, you need to get a good reference to see what kinds of things you can do with vectors. Here's a good one. The first three items (vector, deque, and list) all deal with lists of things. For what you are doing, use a vector. If you have more than several thousand things in a list, or if you want to be able to add things to the front of the list, use a deque instead. A list can do all these things, but it is a little more than you need to worry about right now.

You are confusing a vector of ints with a vector of student_record classes. ex is an array (or vector) of integers, not student_record classes. And that's why your compiler is complaining about lines 32 and 33.

[edit]^^^^ what Duoas said more detailed than what I said[/edit]

Duoas .. OMG! what i can say .. what agreat great clear explanation :)

really thanx aloOot .. and aloot .. and aloot 4 ur help

thanx 4 the reference .. and about a list .. no no please i think the vector is enough 4 me right now lol :icon_lol:

thanx again :)

--

Ancient Dragon .. thanx 4 ur help ..
i'm sorry about code .. i'll be carefully another time

thanx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.