Thread: searching Vectors

  1. #1
    Registered User
    Join Date
    Oct 2001

    searching Vectors

    Hello. I am having some difficulty trying to search this particular vector. The code is below and how you are to search the vector is reltivly.. >find <anything >
    and whatever is keyed in it will bring up to the screen..
    for instance
    add name1 @address1 #number1
    add name2 @address2 #number2
    add name3 @address3 #number3
    add name4 @address4 #number4
    and then >find name.. it should bring up all the names
    if you can help me out i would gladly appreciate it.

    This program will take in a list of names using a struct.
    It will create these namesinto a database of vectors.
    The functions that can be used in this program are
    add, print delete and find. Each of these functions
    will perform their task such as:'>add name @address #number',
    '>add nmae @address', '>add name #number',
    '>add name #number,@address'. The find function
    can be used such as: find<name, or, address, or phone>.
    The Delete function can be used such as del<entry_number>.
    and the print function can be used such as;
    'print', and the screen will display all names
    addresses and numbers that were in the function.
    The exit function will just exit the program.*/


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

    struct person
    string name;
    string address;
    string number;
    //This is the add function where all the names addresses and phones are taken in
    void addfunc(string s, vector<person>& address1, int add)
    person temp;
    int addrindex, phoneindex;
    addrindex = s.find('@');
    phoneindex = s.find('#');
    if(addrindex!=-1 || phoneindex!=-1)
    //add name @address #number
    //finds the position of add and takes in everything after that to a string, addrindex-(add+3));
    temp.address=s.substr(addrindex+1, phoneindex-(addrindex+1));
    temp.number=s.substr(phoneindex+1, s.length()-(phoneindex+1));

    //add name #number @address
    if (addrindex>phoneindex)
    {, phoneindex-(add+3));
    temp.address=s.substr(addrindex+1, s.length()-(addrindex+1));
    temp.number=s.substr(phoneindex+1, addrindex-(phoneindex+1));

    //add name #number
    {, phoneindex-(add+3));
    temp.address=" ";
    temp.number=s.substr(phoneindex+1, s.length()-(phoneindex+1)); //phone index is the pound sign
    //add name @address
    temp.number=" ";
    temp.address=s.substr(addrindex+1, s.length()-(addrindex+1));

    // stores the vectors here

    //This is the print function.
    //It will display all the names addresses and phones of the persons entered
    void print1(const vector<person>& address1)

    for (int i= 0; i < address1.size(); i++)
    // cout << i+1 << " " << cout.width(10) << address1[i].name<< cout.width(20) <<" " << address1[i].address << cout.width(30)<<" " << address1[i].number << endl;
    // cout.setf(ios::left);

    cout<< i+1 <<") ";
    //cout<<i+1 <<" "<< address1[i].name<<cout.width(10) <<address1[i].address<<cout.width(10)<<address1[i].number<< cout.width(10)<<endl;

    //this function will delete a specific line number in the vector
    void delfunc(int i, vector<person>& address1, int del,string s)

    string del_string;
    //takes the interger del and adds three to it to get the position.
    //Then it takes the length of the rest of the sting and
    //subtracts the position of del to get what the actual string is.
    del_string=s.substr(del+3, s.length()-(del+3));
    //Here it converts the string into an integer to get
    //the number of what the user wants deleted
    i = atoi(del_string.c_str());
    if ((i<1) || (i>address1.size()))
    cout<< "Please re-enter in another selection "
    "becuase you entered in an invalid number \n"
    "Try something like del 2 "
    "If that doesn't work and you get the same message \n"
    "You are an idiot and don't deserve to be living \n"
    "Re-read the specifations for this entry \n";

    else if ((i>=1) || (i<=address1.size()))

    for (int j = i-1; j < address1.size() - 1; j++)
    address1[j].name = address1[j+1].name;
    address1[j].address = address1[j+1].address;
    address1[j].number = address1[j+1].number;


    void findf(vector<person>& address1, int fin,string s)
    string find_string;
    find_string=s.substr(fin+4, s.length()-(fin+4));
    int check;
    for (int i = 0; i < address1.size(); i++)
    check = address1[i].name.find(find_string);
    if (check != string::npos)
    //print current person

    //This function exists the entire program and gives a corcy message
    void func_exit(int ex)
    cout<<"You have just exited out of the "
    "program in which you just were working on\n"
    "If you did no which to do this then you must\n"
    "Re-build the entire code other wise "
    "have a nice day.\n"
    "If you build it, they will come. "
    "But if you spin it, they will dance.\n\n";
    void func_menu(int menu)
    string input = "no";
    while(input!= "yes")
    cout<<"You have chosen to use the menu\n ";
    cout<<"are you sure you want to choose the menu? "<<endl;
    cout<<"please enter yes or no ";
    if(input!= "yes")
    cout<<"please re-enter your choice ";
    cout<<"please enter yes or no \n";
    cout<<"Make a 'yes' choice god-damnit\n";

    cout<<"\t use 'add' to add to the address book,\n"
    "\t use 'del to delete a name,\n"
    "\t use 'find' to find a name in the phone book\n"
    "\t use 'menu' to get the menu \n"
    "\t use 'exit' to exit the program\n"
    "\t thank you for playing\n\n";

    //The main function where all the other functions are called
    void main()
    cout<<"Welcome to AddressBook v(1.003)\n"
    "We welcome any suggestions to help improve\n"
    "our course to develop better work for you\n\n"
    "The menu options are as follows: \n\n"
    "\t use 'add' to add to the address book,\n"
    "\t use 'del to delete a name,\n"
    "\t use 'find' to find a name in the phone book\n"
    "\t use 'menu'to get the menu \n"
    "\t use 'exit' to exit the program\n"
    "\n\t\t\tIf you have any questions or comments \n"
    "\t\t\tplease send your regards to\n"
    "\t\t\tPlease come visit us soon at \n"
    "\t\t\tsince you know how much we care \n";

    char a[80];
    string input;
    string del_string;
    vector<person> addressbook;
    int add=0, print=0, del=0,fin=0,ex=0,i=0,menu=0;
    //prints the menu of the address book
    while (1)
    cout<<" >";
    string s=a;
    print = s.find("print");
    if(add > -1)
    addfunc(s, addressbook, add);
    else if (print > -1)
    else if (del >-1)
    else if (fin>-1)
    else if(menu>-1)
    else if (ex>-1)

  2. #2
    Registered User
    Join Date
    Oct 2001
    Try this

    // ref is search string
    for (int i=0; i<vi.size(); ++i)
      if(ref == vi[i])
      cout<<"FOUND: "<<vi[i]<<endl;

    Last edited by Marky_Mark; 10-19-2001 at 08:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM