Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

function not working

Posted on 2000-03-25
9
Medium Priority
?
200 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

571 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