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);
}
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ..
try inserting a r++; in the while and that should work ..
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++;
}
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?
ASKER
i didnt try it as i had server problems, i was just able to post....
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!
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);
}