Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

/*

* - DATA STRUCTURE USING C++ - BUILT USING VISUAL STUDIO 2012


* - Lab Objectives :-
* 1- Structure
* 2- Structure object as array.
* 3- functions with Structure.
* - COM department Second Level
* - By H.Saif
*/

//1- First simple Program


// Structure.cpp : main project file.
#include <iostream>
#include <string>
using namespace std;

// struct Declaration
struct Student{
// variable fields can be accessed from outside
string Name;
int ID;
string Department;
long PhoneNumber;
}student1,student2;/* we can declar objects here before ;*/

int main(){
// 1 - using struct without functions
cout<<"Enter data for first student:\n";
cout<<"Enter the student name: ";
cin>>student1.Name;
cout<<"Enter the student ID: ";
cin>>student1.ID;
cout<<"Enter the student department: ";
cin>>student1.Department;
cout<<"Enter the student phone Number: ";
cin>>student1.PhoneNumber;
//second student
cout<<"Enter data for second student:\n";
cout<<"Enter the student name: ";
cin>>student2.Name;
cout<<"Enter the student ID: ";
cin>>student2.ID;
cout<<"Enter the student department: ";
cin>>student2.Department;
cout<<"Enter the student phone Number: ";
cin>>student2.PhoneNumber;
cout<<"\n\n\t student Data";
cout<<"\nID\tName\tDepartment\tPhone\n";
cout<<student1.ID<<"\t"<<student1.Name<<"\
t"<<student1.Department<<"\t"<<student1.PhoneNumber<<"\n";
cout<<student2.Name<<"\t"<<student2.ID<<"\
t"<<student2.Department<<"\t"<<student2.PhoneNumber<<"\n";
system("PAUSE");//getch(); on dev
return 0;
}

2- second simple program


// Structure.cpp : main project file.
#include <iostream>
#include <string>
using namespace std;

// struct Declaration
struct Student{
// variable fields can be accessed from outside
string Name;
int ID;
string Department;
long PhoneNumber;
};/* we can declar objects here before ;*/
int main(){
//declaring a student as an array of objects
Student s[2];
cout<<"\n\t enter data for 2 students";
for (int i = 0; i < 2; i++){
cout<<"\n Enter the student name";
cin>>s[i].Name;
cout<<"\n Enter the student ID";
cin>>s[i].ID;
cout<<"\n Enter the student department";
cin>>s[i].Department;
cout<<"\n Enter the student phone Number";
cin>>s[i].PhoneNumber;
}
cout<<"\n\t the student data are \nn";
cout<<"\nID \t Name \t Department \t Phone \n\n";
for (int i = 0; i < 2; i++){
cout<<s[i].ID<<"\t"<<s[i].Name<<"\t"<<s[i].Department<<"\
t"<<s[i].PhoneNumber<<"\n";
}
system("PAUSE");//getch(); on dev
return 0;
}

3- Structure With Functions


// Structure.cpp : main project file.
#include <iostream>
#include <string>
using namespace std;
// struct Declaration
struct Student{
// variable fields can be accessed from outside
string Name;
int ID;
string Department;
long PhoneNumber;

};/* we can declar objects here before ;*/


void addStudent(Student s[], int size){
for (int i = 0; i < size; i++){
cout<<"\nEnter the student name: ";
cin>>s[i].Name;
cout<<"\nEnter the student ID: ";
cin>>s[i].ID;
cout<<"\nEnter the student department: ";
cin>>s[i].Department;
cout<<"\nEnter the student phone Number: ";
cin>>s[i].PhoneNumber;
}
}
//function to print one student
void print(Student s){
cout<<"\n\t the student data are \nn";
cout<<"\nID \t Name \t Department \t Phone \n\n";
cout<<s.Name<<"\t"<<s.ID<<"\t"<<s.Department<<"\t"<<s.PhoneNumber;
}
//overloading function
//function to print all student
void print(Student s[],int size){
cout<<"\n\t All student data are \nn";
cout<<"\nID \t Name \t Department \t Phone \n\n";
for (int i = 0; i < size; i++){
cout<<s[i].Name<<"\t"<<s[i].ID<<"\t"<<s[i].Department<<"\
t"<<s[i].PhoneNumber;
}
}
//function to search student by id
void searchStudent(Student s[],int size,long id){
bool found=false;
int index;
cout<<"\nID \t Name \t Department \t Phone \n\n";
for (int i = 0; i < size; i++){
if(s[i].ID==id){
found=true;
index=i;
break;
}
}
if(found){
print(s[index]);//using functions
}
else{
cout<<"\n\n the student with ID = "<<id<<" are not found\n";
}
}
int main(){
//declaring a student as an array of objects
Student s[2];
int choice;
do{
cout<<"\nchoose your choice \n";
cout<<"\n1-insert all student\n2-print all student\n3-search
student\n0-to exit\n\n";
cout<<"\npress tour choice: ";
cin>>choice;
switch(choice){
case 1: addStudent(s,5); break;
case 2: print(s,5); break;
case 3:{
long id;
cout<<"\n Enter student id to search \t";
cin>>id;
searchStudent(s,5,id);
}break;
case 0:goto end; break;
default : cout<<"\n Error choice";
}
}while(choice!=0);
end: // the exit point
cout<<"\n\t end of program";
system("PAUSE");//getch(); on dev
return 0;
}

You might also like