AddRecord

hi im just a beginners. i need help on the add record the question is asking to add only 5 record of students name but avery time the program go to choice menu if click add record the program still add the record. so when display it display all. so i need help only can add 5 students name and if the user click again to add record the program do not add any more record. anyone can help me thanks



#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>

int AddRecord(int i);
int DisplayRecord(int i);
int EditRecord (int i);


struct
{

char stuname1[40];
char stuname2[40];
char stuname3[40];
char stuname4[40];
char stuname5[40];

}mv[100];
int main()
{

int iname=0;
fstream f;
int i=0;
ifstream ifile;
ifile.open("StudentsNames.txt",ios::nocreate);
while(ifile.peek()!=EOF)
{
ifile>>mv[i].stuname1;
ifile>>mv[i].stuname2;
ifile>>mv[i].stuname3;
ifile>>mv[i].stuname4;
ifile>>mv[i].stuname5;
}
ifile.close();
while(true)
{
int ch;
char reply;
system("cls");
cout<<endl;
cout<<"\t\t** WELCOME TO STUDENT NAME REGISTRATION SYSTEM DATABASE **"<<endl;
cout<<endl;
cout<<"\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n"<<endl;
cout<<"\t\t\t-- PLEASE ENTER STUDENTS NAMES --"<<endl;
cout<<endl;
cout<<"\t\t\t1.Add Students Names"<<endl;
cout<<"\t\t\t2.Display All The Students Names"<<endl;
cout<<"\t\t\t3.Edit Students Names"<<endl;
cout<<"\t\t\t4.Exit\n"<<endl;
cout<<endl;
cout<<"\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
cout<<"\t\t\t Enter your Choice (1-4)\n\n";
cin>>ch;
switch (ch)
{
case 1:

i=AddRecord(i);
break;
if(reply=='1')
system("pause");
break;

case 2:
DisplayRecord(i);
break;
case 3:
EditRecord(i);
break;
case 4:
cout<<"\n\n Please Check All The Students Name Before You Exit This Program"<<endl;
cout<<"\t\t\t\tTHANK YOU"<<endl;
exit(1);
break;
default:
cout<<"\n\nlnvalid choice!!!"<<endl;
system("pause");
break;


}
}
return 0;
}


int AddRecord(int i)
{
int iname=0;
char reply;
int x;
ofstream ofile;
while(1)


{
system("cls");
cout<<"\t\t**************Add Students Names***************\n\n"<<endl;

cout<<"Enter 1st Student Name :"<<endl;
cin>>mv[i].stuname1;
cout<<"Enter 2nd Student Name :"<<endl;
cin>>mv[i].stuname2;
cout<<"Enter 3rd Student Name :"<<endl;
cin>>mv[i].stuname3;
cout<<"Enter 4rd Student Name :"<<endl;
cin>>mv[i].stuname4;
cout<<"Enter 5th Student Name :"<<endl;
cin>>mv[i].stuname5;
i++;
cout<<"DATA ADDED SUCCESSFULLY!\n";
cout<<endl;


//saving record
ofile.open("SaveData.txt",ios::trunc);
for(x=0;x<i;x++)
{
ofile<<mv[x].stuname1<<endl;
ofile<<mv[x].stuname2<<endl;
ofile<<mv[x].stuname3<<endl;
ofile<<mv[x].stuname4<<endl;
ofile<<mv[x].stuname5<<endl;

}

cout<<"\nContinue? (y/n):";
cin>>reply;
if(reply=='n')
{
return i;
break;
}

return i;


}
}

int DisplayRecord(int i)
{

int y;
system("cls");
cout<<"\t\t**************Display Students Names***************\n\n"<<endl;
for(y=0;y<i;y++)

{
cout<<"\t\t1st Student Name \t\t="<<mv[y].stuname1<<endl;
cout<<"\t\t2nd Student Name \t\t="<<mv[y].stuname2<<endl;
cout<<"\t\t3rd Student Name \t\t="<<mv[y].stuname3<<endl;
cout<<"\t\t4rd Student Name \t\t="<<mv[y].stuname4<<endl;
cout<<"\t\t5th Student Name \t\t="<<mv[y].stuname5<<endl;
}
cout<<"\t\t------End Of Report------\n\n"<<endl;
system("pause");
return 0;
}
int EditRecord(int i)
{
int x;
int iname=0;
ofstream ofile;
if (i<=0)
{
cout<<"\nNothing to Edit!\n";
system("pause");
return i;
}
int z;
system("cls");
cout<<"\t\t**************Edit Students Names***************\n\n"<<endl;
{
}
for(z=0;z<i;z++)
{

{

cout<<"OLD 1st Student Name :"<<mv[z].stuname1;
cout<<endl;
cout<<"NEW 1st Student Name:";
cin>>mv[z].stuname1;
cout<<endl;
cout<<"OLD 2nd Student Name :"<<mv[z].stuname2;
cout<<endl;
cout<<"NEW 2nd Student Name:";
cin>>mv[z].stuname2;
cout<<endl;
cout<<"OLD 3rd Student Name :"<<mv[z].stuname3;
cout<<endl;
cout<<"NEW 3rd Student Name:";
cin>>mv[z].stuname3;
cout<<endl;
cout<<"OLD 4rd Student Name :"<<mv[z].stuname4;
cout<<endl;
cout<<"NEW 4rd Student Name :";
cin>>mv[z].stuname4;
cout<<endl;
cout<<"OLD 5th Student Name :"<<mv[z].stuname5;
cout<<endl;
cout<<"NEW 5th Student Name:";
cin>>mv[z].stuname5;

cout<<endl;

cout<<"DATA EDITED SUCCESSFULLY!";
system("pause");
}
}
ofile.open("StudentsNames.txt",ios::trunc);
if(ofile.fail())
{
cout<<"error writting to directory\n\n";
}
for(x=0;x<i;x++)
{
ofile<<mv[x].stuname1<<endl;
ofile<<mv[x].stuname2<<endl;
ofile<<mv[x].stuname3<<endl;
ofile<<mv[x].stuname4<<endl;
ofile<<mv[x].stuname5<<endl;

}
ofile.close();
return 0;
}
Last edited on
Topic archived. No new replies allowed.