Solved

# function looping forever

Posted on 2000-03-28
238 Views
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 {

}
}while (r < i);
}
0
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

LVL 1

Expert Comment

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

}

0

Accepted Solution

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";

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

return;
}

0

Expert Comment

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

Author Comment

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 )   {
}
else {
cout << endl << r << ". " << directory[r].name << " " << directory[r].number;
r++;
}
}

//r++;
}
0

Expert Comment

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

Author Comment

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

Author Comment

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

Question has a verified solution.

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

### Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month2 days, 2 hours left to enroll