IF within FOR problems

Hi I am currently trying to write a program to input details of customers into an array, which is saving to a binary file then edit them. I am using a for loop to search through the array for a custno, I only have 3 records at the moment for testing purposes. When I enter a custno which does'nt exist it displays a message but as the if(cuno == customers[i].custno) is in the array it displays this message 3 times. I have tried an else with a break but this stops any valid input after entering a custno once. Sorry for the long winded explanation, Here the edit function of the code

void editRec(cust customers[], int &row)
{
clrscr();

int entry;
int cuno = 0;
char yeno;
boolean found = false;
cout << "Enter Customer Number to Edit : " ;
Input(cuno);

for ( int i=0;i<row;i++ )
{
if (cuno == customers[i].custno)
{
boolean found = true;
cout << "Record Found:\n"
<<"1 Cust No :"<<customers[i].custno <<endl
<<"2 Cust Name:"<<customers[i].name << endl
<<"3 Street :"<<customers[i].street << endl
<<"4 Town :"<<customers[i].town << endl
<<"5 Postcode :"<<customers[i].postcode << endl
<<"6 Area Code:"<<customers[i].phone << endl
<<"7 Phone No :"<<customers[i].number << endl
<<"8 Total :"<<customers[i].totsale << endl
<<"9 Credit :"<<customers[i].credlim <<endl
<<"0 Balance :"<<customers[i].curbal <<endl
<<"\nAre you sure you want to edit this record?"<<endl;
Input(yeno);

if(yeno =='y'||yeno == 'Y')
{

cout << "\n\nEnter line number of record to be Edited:";
cin >> entry;
switch (entry)
{
case 1: clrscr();
do
{
cout << "Please Enter Customer Number : ";
Input (customers[i].custno);
}
while(customers[i].custno < 0 || customers[i].custno > 500);
break;
case 2: clrscr();
cout << "\n\nPlease Enter Customers Name : ";
Input (customers[i].name,31);
break;
case 3: clrscr();
cout << "\n\nPlease Enter Customers Street : ";
Input (customers[i].street,26);
break;
case 4: clrscr();
cout << "\n\nPlease Enter Customers Town : ";
Input (customers[i].town,17);
break;
case 5: clrscr();
cout << "\n\nPlease Enter Customers Postcode : ";
Input (customers[i].postcode,9);
break;
case 6: clrscr();
cout << "\n\nPlease Enter Customers Phone code : ";
Input (customers[i].phone,6);
break;
case 7: clrscr();
cout << "\n\nPlease Enter Customers Phone Number : ";
Input (customers[i].number,7);
break;
case 8: clrscr();
cout << "\n\nPlease Enter Customers Total Sales ex VAT : ";
Input (customers[i].totsale);
break;
case 9: do
{
clrscr();
cout << "\n\nPlease Enter Customers Credit Limit : ";
Input (customers[i].credlim);
}
while (customers[i].credlim < 0.00 || customers[i].credlim > 9999.99);
break;
case 0: clrscr();
cout << "\n\nPlease Enter Customers Current Balance : ";
Input (customers[i].curbal);
break;
default: clrscr();
cout << "Invalid option\n";
pressKey();
break;
}
cout << "\nRecord Updated returning to main menu";
pressKey();

}
else
{
errorMsg("Returning to Add, Delete & Edit Menu");
break;
}
}
else
errorMsg("Wrong num");
//pressKey();
break;
}

}
try doing somethisg like:
1
2
3
4
5
6
7
8
9
10
bool found=0;
for ( int i=0;i<row;i++ ){
    if (cuno == customers[i].custno){
        found=1;
        //do whatever you want
    }
}
if(!found){
    //display your message
}

that way the program will only print the message once if no customers were found.
I'll give it a try...

Thank you!
It worked hamsterman,

I knew it'd be easy jus' could'nt get my head round it.

Thank you very much, its appreciated.

Kev
Topic archived. No new replies allowed.