C++ exercises and solutions: add new record to list


C++ structure exercise: add new record to list

Step 3: Defining the add_rec(struct student[] st, int& itemcount) function to add a new record to the the array of student objects. This method takes two arguments. The first argument is the array of student objects(st) and the second argument is the number of items in the array. This function firstly checks the new record(using the search function that is defined in the next step) before it is allowed to be appended to the array to avoid duplicate records. When the new item is added the value of itemcount variable increases by 1 that means the number of records in the list increases.


void add_rec(struct student st[],int& itemcount){

again:
cout<<"\nEnter student's ID:";
cin>>st[itemcount].stnumber;
if(search(st,st[itemcount].stnumber,itemcount)!=-1){
cout<<"This ID already exists\n";goto again;
}

cout<<"Enter student's Name:";
cin>>st[itemcount].stname;
cout<<"Enter student's Sex(F or M):";cin>>st[itemcount].sex;
cout<<"Enter student's quizz1 score:";cin>>st[itemcount].quizz1;
cout<<"Enter student's quizz2 score:";cin>>st[itemcount].quizz2;
cout<<"Enter student's assigment score:";cin>>st[itemcount].assigment;
cout<<"Enter student's mid term score:";cin>>st[itemcount].midterm;
cout<<"Enter student's final score:";cin>>st[itemcount].final;
st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+st[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;

++itemcount;


}



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.