Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

function not working

Posted on 2000-03-25
9
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 5

Expert Comment

by:pitonyak
ID: 2657550

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
ID: 2657576
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
ID: 2657683
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:weinrj
ID: 2657691
if its not found, it should increment r by one, do the check again, right?  i think i got the algorithm right.
0
 

Accepted Solution

by:
PKothari earned 1200 total points
ID: 2657945
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
ID: 2658647
Adjusted points from 200 to 300
0
 

Author Comment

by:weinrj
ID: 2658648
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
ID: 2664031
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
ID: 2664657
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

660 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