PDA

View Full Version : Last C++ Question for a while


mscags
12-08-2005, 12:34 AM
I've got my program pretty much completed except for the search algorithm. I need to let the user imput a name or partial name and search for it. I also need to let the user repeat this if they want. I'm pretty sure that means I need to make the search a function that way it is easily repeated. ANyway, I have no idea how to let the user search for a name or partial name. If anyone has any ideas please let me know. Also if you see anything wrong please tell me.

//This program will let the user imput names and phone numbers and will then let

//the user search for names and will display the reults. The user will then be able

//to repeat this if they desire.

#include <iostream>

#include <string>

using namespace std;

int main()

{

char Entries = 0;

char response[13];

char Numbers = 0;

char Search;

cout << "How many entries do you have? ";

cin.getline(response,256);

Entries = atoi(response);

string *PtrNames = new string[Entries];

string *PtrNumbers = new string[Entries];

for ( int row=0; row<Entries; row++)

{

cout << "Enter name #" << row+1 << " ";

cin.getline(response,13);

PtrNames[row] = response;

}

cout << "The " << Entries << " names that you entered are " << endl;

for (int row = 0; row<Entries; row++)

{

cout << PtrNames[row] << endl;

}

for ( int row=0; row<Entries; row++)

{

cout << "Enter Phone Number #" << row+1 << " ";

cin.getline(response,13);

PtrNumbers[row] = response;

}

for (int row =0; row<Entries; row ++)
{
for (int i=0; i<1; i++)
{
if (PtrNumbers[row][i] < '0' || PtrNumbers[row][i] >'9')
{
cout << "Phone number #"<< row+1<< " is invalid\n";
cout << "Please restart this program\n";
}
else
if
(PtrNumbers[row][4] != '-' && PtrNumbers[row][7] != '-')
{
cout << "Phone number #"<<row+1<< " is invalid\n";
}

}

}

cout << "The " << Entries << " phone numbers that you entered are " << endl;

for (int row = 0; row<Entries; row++)

{
cout << PtrNumbers[row] << endl;
}

cout << "What name would you like to search for?\n";
cin >> Search; //I have no idea where to go from here

int SeeResults;
cin >> SeeResults;

return 0;

}

rt1
12-08-2005, 01:47 AM
loop thru array (for loop),

if input name matches that name stored in the array, we have a winner...

ps, your validation of the phone number sucks...

mscags
12-08-2005, 04:04 AM
[ QUOTE ]
if input name matches that name stored in the array, we have a winner...

[/ QUOTE ]
will that check for partial matches though?

[ QUOTE ]

ps, your validation of the phone number sucks...

[/ QUOTE ]

Any suggestions?

pokergrader
12-08-2005, 07:39 AM
[ QUOTE ]
[ QUOTE ]
if input name matches that name stored in the array, we have a winner...

[/ QUOTE ]
will that check for partial matches though?


[/ QUOTE ]

Once you clearly define the meaning of "partial matches" for yourself, you have enough understanding of loops to write the code.

mason55
12-09-2005, 02:18 AM
http://www.cppreference.com/cppstring/find.html