Solved

function not working

Posted on 2000-03-25
9
175 Views
Last Modified: 2010-04-02
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;
}
0
Comment
Question by:weinrj
9 Comments
 
LVL 5

Expert Comment

by:pitonyak
Comment Utility

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
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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.
0
 

Author Comment

by:weinrj
Comment Utility
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);
}
0
 

Author Comment

by:weinrj
Comment Utility
if its not found, it should increment r by one, do the check again, right?  i think i got the algorithm right.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Accepted Solution

by:
PKothari earned 300 total points
Comment Utility
void listName(phone []) {
cout << "\nEnter an extention to list a name: ";
    char j[5];
int r = 0;
int flag=0;
cin >> j;
cout << "\nSearching...\n";
do {
if (strcmp(directory[r].number, j) == 0)
{
flag=1;
cout << endl << directory[r].name << endl;
break;
}
    else
         r++;
} while(!flag);
}
Well in your case since do-while is an exit controlled loop if the condition satisfies it comes out of the loop.
Hence only once.
0
 

Author Comment

by:weinrj
Comment Utility
Adjusted points from 200 to 300
0
 

Author Comment

by:weinrj
Comment Utility
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);
}
      
0
 

Expert Comment

by:gshankar
Comment Utility
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 .
0
 

Author Comment

by:weinrj
Comment Utility
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.  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now