Solved

function looping forever

Posted on 2000-03-28
7
234 Views
Last Modified: 2010-05-19
this is supposed to find all numbers that belong to a name....it looks, and finds if more exists, if it does, it supposed to print it out...if there are no, it gives an error...it was doing both before then i resturced the if and it well, doesnt work anymore....arg....any ideas?  thnx


void listNumber(phone [], int & i) {
      cout << "\nEnter name to find extention: ";
      char j[16];
      int r = 0;
      cin >> j;
      cout << "\nSearching...\n";
      
      do {
            if ( strcmp ( directory[r].name, j ) == 0 ) {
                  cout << endl << r << ". " << directory[r].name << " " << directory[r].number;
            }
            
            
                        
            else {
                  cerr << "\nError 320: Not found.\n\n";
                menu(i);
                           
            }
      }while (r < i);
}
0
Comment
Question by:weinrj
7 Comments
 
LVL 1

Expert Comment

by:sunj
ID: 2666076
The logic is a bit messy there.. Try this:


void listNumber(phone [], int & i) {
cout << "\nEnter name to find extention: ";
char j[16];
int r = 0;
cin >> j;
cout << "\nSearching...\n";

do {
if ( strcmp ( directory[r].name, j ) == 0 ) {
cout << endl << r << ". " << directory[r].name << " " << directory[r].number;
return;
}
r++;
}while (r < i);


cerr << "\nError 320: Not found.\n\n";
     menu(i);
}    

0
 

Accepted Solution

by:
vhawargi earned 50 total points
ID: 2666367
note that "sunj"'s code stops at giving only one number.
 try this code. i am using a flag. if the flag is never set (or is zero, if considered as counter) then you can

print out the error message.

void listNumber(phone [], int & i)
{
   cout << "\nEnter name to find extention: ";
   char j[16];
   int r = 0;
   cin >> j;
   cout << "\nSearching...\n";
   // your code till here
   
   int fFound = 0;  // a counter flag

   do {
         if ( strcmp ( directory[r].name, j ) == 0 )
         {
                  cout << endl << r << ". " << directory[r].name << " " << directory[r].number;
            fFound++;  // increment to indicate we found..
         }
      r++;
    } while ( r < i) ;

    if (!fFound)   // if the flag never got incremented... (we can also NAND r's value as an exceptional handler)
    {
      cerr << "\nError 320: Not found.\n\n";
      menu(i);
    }

return;
}
   


0
 

Expert Comment

by:manjit
ID: 2666998
you never incremented the value of r so it will just keep checking the same reconrd again and again ..!!

try inserting a r++; in the while and that should work ..
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:weinrj
ID: 2667858
current problems:  only finds first record and still prints error, doesnt find anything else

void listNumber(phone [], int & i) {
      cout << "\nEnter name to find extention: ";
      char j[16];
      
      cin >> j;
      
      cout << "\nSearching...\n";
      int r = 0;
      while ( r < i) {
      if ( strcmp ( directory[r].name, j ) != 0 )   {
           cerr << "\nError 320: Not found.\n\n";
           menu(i);
        }
      else {
           cout << endl << r << ". " << directory[r].name << " " << directory[r].number;
               r++;
        }
      }
      
      //r++;
}
0
 

Expert Comment

by:vhawargi
ID: 2667875
did you try my code that i had given earlier? does it differ from your requirements?
0
 

Author Comment

by:weinrj
ID: 2668092
i didnt try it as i had server problems, i was just able to post....
0
 

Author Comment

by:weinrj
ID: 2669863
Many thnx...I was going to use a flag that controlled the error but didnt know exactly what to do with it or how, many thnx!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

747 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