Link to home
Start Free TrialLog in
Avatar of weinrj
weinrj

asked on

function looping forever

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);
}
Avatar of sunj
sunj

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);
}    

ASKER CERTIFIED SOLUTION
Avatar of vhawargi
vhawargi

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
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 ..
Avatar of weinrj

ASKER

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++;
}
did you try my code that i had given earlier? does it differ from your requirements?
Avatar of weinrj

ASKER

i didnt try it as i had server problems, i was just able to post....
Avatar of weinrj

ASKER

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!