Link to home
Start Free TrialLog in
Avatar of weinrj
weinrj

asked on

function not working

enclosed are two functions...the top one is where the problem is , the last one is useful as well to fix the problem.  i have a struct...however, how do i append the '\0' to the end of both char arrays to make it a string?  i think this is why my first function is causing to be ifinite and the 2nd function is where the string attaschments are requird....please help, thxn

void listNumber(phone []) {
      cout << "Enter name to find extention: ";
      char j[16];
      int r = 0;
      cin >> j;
      cout << "\nSearching...\n";
      do {
            if (directory[r].name == j)
                  cout << directory[r].number;
            else
                  r++;
      }while (directory[r].name != j);
}
      


void openFile(phone directory[],int &i) {  
      
      ifstream incoming;
      incoming.open("phone.dat");
    if (!incoming) {
     cerr << "You made a mammouth sized mistake and the computer is now giving up, so there!\n";
     exit(1);
  }

      do {
          incoming >> directory[i].name >> directory[i].number;
                        
            i++;
      } while (!incoming.eof());
     
    incoming.close;      
      cout << endl << endl;       
      int k = i;
}
Avatar of pitonyak
pitonyak


I believe that the real problem is

if (directory[r].name == j)

note that in reality, j is a pointer and I am guessing that name is also a pointer.

try

if (strcmp(directory[r].name, j) == 0) {
}

Note that this will do a case sensitive compare and there will be problems if either of the pointers are NULL.

You could also use strncmp() perhaps and some compilers offer a case insensitve compare of stricmp() or strcmpi().

I can provide more information if you desire.

Andy
pitonyak, is correct.  In C/C++ arrays cannot be compared with the == != <= etc operators.  These operations just compare the addresses (pointers) of the arrays, which usually is not what you want.  In order to compare the contents of the arrays, you need to compare each data member inside the array or us a function that does so.  The strcmp() function is one that does this comparison for character arrays.

Similarly, you cannot copy arays using the = operator.  You need to write code that copies each data item inside the array, or again call some sort of function that does this.  The strcpy() function does this for character arrays.

However, if you use a string class, instead of a character array to store your string syou will be able to use the relational operatos (==, 1=, <, > etc) to compare strings and you will be able to use operator = to copy a string.  There are also many other advantages to using string classes.  so you shoudl seriously consider it.  The stl string class would be a good one to try.
Avatar of weinrj

ASKER

hmm interesting...it appears that when i ran the function, it only found the first person in the struct, it didnt increment.  this is what i now have....


void listName(phone []) {
      cout << "\nEnter an extention to list a name: ";
    char j[5];      
      int r = 0;
      cin >> j;
      cout << "\nSearching...\n";
      do {
            if (strcmp(directory[r].number, j) == 0)
                  cout << endl << directory[r].name << endl;                  
          else
              r++;
      } while (strcmp(directory[r].number, j) == 1);
}
Avatar of weinrj

ASKER

if its not found, it should increment r by one, do the check again, right?  i think i got the algorithm right.
ASKER CERTIFIED SOLUTION
Avatar of PKothari
PKothari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weinrj

ASKER

Adjusted points from 200 to 300
Avatar of weinrj

ASKER

thank you....however, before i reward points, there are two slight problems...

if there is more than one name when searching by name, it will stop at the first one and quit....so both need to be listed....and for both functions, if the name/extention isnt found, c++ crashes with a hex address cant read something another...so i need to give an error if its not found.  thnx.......

void listNumber(phone []) {
      cout << "\nEnter name to find extention: ";
      char j[16];
      int r = 0;
      int flag = 0;
      cin >> j;
      cout << "\nSearching...\n";
      do {
            if (strcmp(directory[r].name, j) == 0) {
                  flag = 0;
                  cout << endl << directory[r].number << endl;
                  break;
            }

            else
                  r++;
      }while (!flag);
}
      
Yes, I can help u out with the 2 probs.

Do not quit the search loop when u find the first occurence of the name .
Instead the while condition should be the end of file ( i.e, the dat file's end)
I can give u the algo.

flag=0;
get the name ;
open file;
do
{
/*search for name from the start of file;*/
if (directory[r].name equalto name)
{
 output directory[r].number;
 set flag=1;
}
}while(!eof(datfile));
if flag==0
{
cout<<name not in directory;
exit;
}

hope u understand the algo;
get back if u still have doubts;
I can give u the actual program if neccessary .
Avatar of weinrj

ASKER

the actual source isnt neccessary, i do understand the algorithm basicly, i just used the break as suggested.  but seeing the code rather than pseudo would make more sense to me, as thats out my text book teaches....they show source and the following 20 pages in teh chapter goes into detail what each line is doing and why.