FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help With C++ Searching





Jamestf347
Well I've been working on this phone book sort of c++ program for my high school class, It incorporates everything we've learned so far ... The only problem is I never got a hand of searching before we went by it. So with that said, Can anyone help me fix my search method? I don't see a problem in it, but then again I said I don't fully get search. Thank you for Who Ever Helps me, I'll make it worth your while Smile.

Code:

void searchbyname()
{
   if (retrieved==1)
        {
   int i, flag1, flag2, searchammount;
   char searchname1[30], searchname2[30], found, found2;
   clrscr();

   cout<<"Compiling Data...";
   delay(700);
   clrscr();
   cout<<"Searching By Name...";
   delay(700);
   
   clrscr();
   do
   {
    clrscr();
    cout<<"Please Enter The First Name To Search For: ";
    cin>>searchname1; //first name temporary search variable

    clrscr();
    cout<<"Please Enter The Last Name To Search For: ";
    cin>>searchname2; //last name temporary search variable

   flag1=1;
        flag2=1;
   for (i=1; i<=num; i++)
   {
   found=stricmp(firstname[i], searchname1[1]); // Comparing The first names in file with the first name to be searched
   found2=stricmp(lastname[i], searchname2[1]); // Comparing The last names in file with the last name to be searched
   
       if (found == 0)
      {
       clrscr();
       cout<<"\n"<<searchname1<<" In Slot number "<<i; // firstname found
       getch();
                 flag1=0;
      }

       if (found2 == 0)
      {
       clrscr();
       cout<<"\n"<<searchname2<<" In Slot number "<<i; //lastname found
       getch();
                 flag2=0;
      }
   }

      else if (flag1 == 0 && flag2 == 0) //not found if statement
      {
       clrscr();
       cout<<searchname1[1]<<" "<<searchname2[1]<<" Not Found";
         gotoxy(50, 18);
         cout<<"Press Enter To Continue!";
       getch();
      }

    clrscr();
    cout<<"Would You Like To Search For Anymore Names? (y/n): ";
    cin>>choice1;

   } while (choice1 == 'y' || choice1 == 'Y');
}
   if (retrieved == 0)
   {
    clrscr();
    cout<<"PLEASE RETRIEVE DATA BEFORE USING THIS OPTION!";
    getch();
    mainmenu();
        }
}


** IGNORE SOME OF THE VARIABLES USED, THEY MIGHT IN FACT BE GLOBAL, JUST NOT SHOWN HERE.

Once again I say thank you for anyone willing to help.
Peterssidan
It's hard when we don't know what values some variables has. What value does num has for instance?

It looks like you are doing something wrong with the flags (flag1 and flag2). If a name is found you set a flag to zero which is unnecessary because you already know it's zero. Probably you want to set it to something else because later you check if flag1 is zero (why not also check flag2?). If it is zero you print "Not Found", which will happen all the time.
Jamestf347
Yeah Sorry I didn't Put Up The Most Recent Search void I made I fixed the flags in the most recent and it still won't search. num is the number which it retrieves from the diskfile. It tells how many users are entered, searchname1, and searchname2 are the temporary variables used to search.
fladry
Why don't you use string instead of using char?
Code:
string searchname1
is much better than
Code:
char searchname1[30]
Are you using turbo c++?
Jamestf347
I only use char cause that's the way I was taught. But yes I'm using Turbo C++.
fladry
i think that turbo c++ doesn't have a string class with much functionality. And,
Code:

stricmp(lastname[i], searchname2[1])
compares two characters and not whole strings. Did you intended that? Sorry if i am wrong. I don't know handling chars with c functions much. But i think that you should have done
Code:
strcmp(lastname, searchname2)
And, another thing,
Code:

 if (found == 0)
      {
       clrscr();
       cout<<"\n"<<searchname1<<" In Slot number "<<i; // firstname found
       getch();
                 flag1=0;
      }

       if (found2 == 0)
      {
       clrscr();
       cout<<"\n"<<searchname2<<" In Slot number "<<i; //lastname found
       getch();
                 flag2=0;
      }

If both first name and second name are matched, it displays the slot number twice. So, i think you would better put a 'else' before the second 'if' so that it will compare the last name only if it doesn't find the first. So, your second if statement might look
Code:

else if (found2 == 0)
Jamestf347
I've got it thanks guys Smile. All the problem was, was the carriage returns. I needed to add a carriage return to the char variable, before searching because there was one on the variable being searched Smile.
Related topics
HELP ME in C++
Need help in Borland C++ 3.0
Frihost DC (Distributed Computing) Project
Comuter stopped recognizing my Camera USB
C++ assignment help
The Whole "GOD" Thing
Yabb problem
mail system......an mysql....
Webmasters needed - Lock please
How to create a super-computer using clustering in linux?
[help]c++ builder
Linux & Dell C-610 Laptop. Please find me a fan driver.
Two problems... need help, bad!
Help: C++ : windows.h
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.