searching Vectors

• 10-18-2001
minikg
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
and then >find name.. it should bring up all the names
if you can help me out i would gladly appreciate it.
thanks
karen
kginsberg@hotmail.com

/*
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
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>
#include<iomanip>
using namespace std;

struct person
{
string name;
string number;
};
//This is the add function where all the names addresses and phones are taken in
{
person temp;
phoneindex = s.find('#');
{
{
//finds the position of add and takes in everything after that to a string
temp.number=s.substr(phoneindex+1, s.length()-(phoneindex+1));

}
{

}
}
{
temp.number=s.substr(phoneindex+1, s.length()-(phoneindex+1)); //phone index is the pound sign
}
if(phoneindex==-1)
{
temp.number=" ";
}

// 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.width(20);
cout.width(17);
cout<<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++)
{
}
}

}

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++)
{
if (check != string::npos)
{
//print current person
break;
}
}
}

//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";
exit(-1);
}
{
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 ";
cin>>input;
if(input!= "yes")
{
cout<<"please enter yes or no \n";
cin>>input;
cout<<"Make a 'yes' choice god-damnit\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 '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 come visit us soon at http://www.something.com \n"
"\t\t\tsince you know how much we care \n";

char a[80];
string input;
string del_string;
//prints the menu of the address book
while (1)
{
cout<<" >";
cin.getline(a,80);
string s=a;
print = s.find("print");
del=s.find("del");
fin=s.find("find");
ex=s.find("exit");
else if (print > -1)
else if (del >-1)
else if (fin>-1)
else if (ex>-1)
func_exit(ex);
}
}
• 10-18-2001
Marky_Mark
Try this

Code:

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

Marky_Mark