با سلام خدمت اساتید
من یه دفترچه تلفن در c++ نوشتم. اما یه مشکل داره. اونم اینه ، زمانی که از منوی حذف یا ویرایش رکورد بر میگردم به منوی اصلی و میخوام رکورد جدید وارد کنم. به جای اینکه ابتدا اسم رو بگیره بعد شماره; مستقیما بعد از زدن اینتر از اسم میگذره و میره سراغ شماره تلفن.



# include <iostream.h>
# include <conio.h>
# include <stdio.h>
# include <string.h>
# include <fstream.h>
int maxlist;
char name[50][100];
char number[50][100];
char a;
char P[50];
char K[50];
int i;
char s[50];
int found;
char Editname[50];
char Editnumber[50];
char menu[8][19]={" Enter data: ",
" Show All List: ",
" Save: ",
" Search: ",
" Delete Record: ",
" Edit Record: ",
" Sort A-Z: ",
" Exit: "};


void displayData()
{
clrscr();
for(int i=0; i<100;i++)
{ gotoxy(1,5+i);
textcolor(YELLOW);
textbackground(1);
cprintf("||"); }//for
for(int i=0; i<100;i++)
{ gotoxy(55,1+i);
textcolor(YELLOW);
textbackground(1);
cprintf("||"); }//for
//-------------------------------------
gotoxy(1,1);
textcolor(YELLOW);
textbackground(1);
cprintf(" ----------- NAME ---------------- MOBILE ------------");
cout<<endl<<endl;
for (int i=0;i<maxlist; i++)
{
gotoxy(3,3+i);
textcolor(YELLOW);
textbackground(0);

cprintf("\n\n ");cout<<i;cprintf("-");cout<<name[i];cprintf("...........");
cout<< number[i];
// cout <<endl<<"\n\n "<< i<< "-"<< name[i]<<"........."<<number[i];

}
cout <<endl<<endl;

textcolor(12);
textbackground(0);
cprintf( " press any key to continue...." ) ;

getch();
}

//-------------------------------------------------displaydata()

void loadData()
{
ifstream fp("DATA118.txt") ;
while (fp>> name[i]>>number[i])
{
maxlist++; i++;
}

fp.close();
displayData();
cout << endl<< "tedad record: " << maxlist;
}

//--------------------------------------------------loadData()

void EnterData()
{
if(maxlist>2000)

{
gotoxy(5,20);
textcolor(LIGHTRED);
textbackground(0);

cprintf(" the list is full. please enter any key to continue ");
getch();}

else
{
gotoxy(5,20);
textcolor(YELLOW);
textbackground(0);
cprintf( "\n enter name: ");

cin.getline(P,50);
strcpy(name[maxlist],P) ;

gotoxy(5,23);
textcolor(YELLOW);
textbackground(0);
cprintf( "\n enter phone number: ");
cin.getline(K,50);
strcpy(number[maxlist],K) ;
maxlist++;
}

}

//------------------------------------------------enterdata()
void SortData()
{
char temp[50];
char temp2[50];
for(int j=0;j<maxlist;j++)
for(int i=0;i<maxlist;i++)
{
if( strcmpi(name[i],name[i+1])>0 )
{
strcpy(temp,name[i]); strcpy(temp2,number[i]);
strcpy(name[i],name[i+1]); strcpy(number[i],number[i+1]);
strcpy(name[i+1],temp); strcpy(number[i+1],temp2);
}
}
displayData();

getch();
}
//------------------------------------------------sortData()








void showMenu(int s)
{ clrscr();
for(int i=0;i<8;i++){
gotoxy(10,5+i);
if(i==s){
textcolor(15);
textbackground(1);
}else
{
textcolor(14);
textbackground(0);
}
cprintf(menu[i]);
gotoxy(70,20);
}

}//------------------------------------------ end showMenu










void EditItem()
{
int newplace;

while(1){
clrscr();
displayData(); //function display
gotoxy(5,maxlist+8);
textcolor(YELLOW);
textbackground(0);

cprintf("\n jahat laghv yek add manfi (e.x -1) vared konid");
gotoxy(5,maxlist+12);
textcolor(YELLOW);
textbackground(0);

cprintf("\n recrod shomare chand edit shavad? : ");



cin>> newplace;
if (newplace<0) break;

gotoxy(5,maxlist+15);
textcolor(YELLOW);
textbackground(1);

cprintf( " Enter Name: ");
cin>>Editname;
strcpy(name[newplace],Editname); //replace new name
//clrscr();
gotoxy(5,maxlist+16);
textcolor(YELLOW);
textbackground(1);
cprintf( "\n\n Enter Number: ");
cin>>Editnumber;
strcpy(number[newplace],Editnumber); //replace new number

}//while

}//------------------------------------------- EditItem

void deleteItem()
{
displayData();
while(1) {
int newplace;
gotoxy(5,maxlist+8);
textcolor(YELLOW);
textbackground(0);
cprintf("\n --------------------------- ");

gotoxy(5,maxlist+11);
textcolor(YELLOW);
textbackground(0);
cprintf("\n list az '0' shoro mishavad " );

gotoxy(5,maxlist+14);
textcolor(YELLOW);
textbackground(0);
cprintf("\n enter '-1' to go to menu ") ;

gotoxy(5,maxlist+17);
textcolor(YELLOW);
textbackground(0);
cprintf("\n recrod shomare chand hazf shavad? : ");
cin>> newplace;
// newplace--;
if (newplace<0) goto END;
for(int i=newplace; i<=maxlist-1;i++)
{ strcpy(name[i],name[i+1]);
strcpy(number[i],number[i+1]);
}

maxlist--;
//cout<<endl<<endl;
clrscr();

displayData();
// for(int i=0;i<=maxlist-1;i++)

// {cout << i<<"."<< name[i]<<"-----"<< number[i]<<endl;}




}//while

END:
getch();
}
//------------------------------------------------deleteItem()


void saveData()
{
ofstream fp("DATA118.txt") ;

for (int i=0;i<maxlist; i++)
{fp<<endl<<name[i]<<endl<<number[i]; }

fp.close();
}

//--------------------------------------------------saveData()
main()

{

maxlist=0;
found=0;
loadData();

char a;
int b,m,n;
m=0;
n=0;

while (1)
{
showMenu(m);
a=getch();
if(a==72)m--;
if(a==80)m++;
if(m<0)m=7;
m=m % 8 ;

if(m==7 && a=='\r') break;
if(m==0 && a=='\r') { EnterData();}
if(m==1 && a=='\r') { displayData(); }
if(m==2 && a=='\r') { saveData();}
if(m==3 && a=='\r') {
gotoxy(2,20);
cprintf("\n please enter number or name: \n");
cin>>s;
for (int i=0; i<maxlist; i++)
{
if (strstr(name[i],s))
{cout <<i<<"-"<< name[i]<< "....." << number[i]<<endl;
found=1;
}
if (strstr(number[i],s))
{cout <<i<<"-"<< name[i]<< "....." << number[i]<<endl;
found=1;
}


// if (strstr(number[i],s))
// cout <<name[i]<< "....." <<number[i];
}//for
if(found==0) cout << endl<< "not found !"<<endl;
cout << endl<< "press enter to continue...";
getch();
}//if


if(m==4 && a=='\r') { deleteItem(); }
if(m==5 && a=='\r') { EditItem(); }
if(m==6 && a=='\r') { SortData(); }
}//for


cout<<"\n\n-----------------------------goodbuy--------------------";
getch();
} //main