Categories
Uncategorised

Structures – Person Data

/ Copyright (C) 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html */

/ Structures – Program to manage person data /

#include <iostream.h>
#include <conio.h>
#include
<stdio.h>
#include <string.h>

struct person
{
int id, age;
char name[50], add[100], nat[50];
} p[20], per[20];

void print (person p[20])
{
int j=1;
cout<<endl<<“Details of people allowed to vote in India”<<endl;
for (int i=0; i<20; i++)
{
if ((p[i].age >= 18) && (strcmp (p[i].nat, “Indian”) == 0))
{
cout<<endl<<“Details for person number “<<j<<” eligible to vote”<<endl<<“Person ID : “<<p[i].id<<endl<<“Person name : “;
puts(p[i].name);
cout<<“Person address : “;
puts(p[i].add);
cout<<“Person age : “<<p[i].age<<endl<<“Person nationality : “;
puts(p[i].nat);
j++;
}
}
}

int search (person p[20], int id)
{
int flag;
for (int i=0; i<20; i++)
if (p[i].id == id)
{
flag = 1;
break;
}
if (flag == 1)
return 1;
else return 0;
}

void arrange (person p[20])
{
person temp[20];
int max, pos;
for (int i=0; i<=18; i++)
{
max = p[i].id;
pos = i;
for (int j=i+1; j<20; j++)
{
if (p[i].id > max)
{
max = p[i].id;
pos = j;
}
}
temp[i] = p[i];
p[i] = p[pos];
p[pos] = temp[i];
}
cout<<endl<<“Person data in descending order of Person ID”<<endl;
for (int k=0; k<20; k++)
{
cout<<endl<<“Data for person “<<k+1<<endl<<“Person ID : “
<<p[k].id<<endl<<“Person name : “;
puts(p[k].name);
cout<<“Person address : “;
puts(p[k].add);
cout<<“Person age : “<<p[k].age<<endl<<“Person nationality : “;
puts(p[k].nat);
}
}

void main()
{
clrscr();
int ch, id, ans;
cout<<endl<<“Program to manage person data”<<endl;
for (int i=0; i<20; i++)
{
cout<<endl<<“Enter data for person “<<i+1<<endl<<“Enter person ID : “;
cin>>per[i].id;
cout<<“Enter person name : “;
gets(per[i].name);
cout<<“Enter person address : “;
gets(per[i].add);
cout<<“Enter person age : “;
cin>>per[i].age;
cout<<“Enter person nationality : “;
gets(per[i].nat);
cout<<endl;
}
cout<<endl<<“Operation Menu”<<endl
<<“1. Print record of all people allowed to
vote in India”<<endl
<<“2. Search for a record using Person ID”<<endl
<<“3. Arrange all records in descending order of ID”<<endl
<<“Enter your choice : “;
cin>>ch;
switch (ch)
{
case 1: print (per);
break;
case 2 : cout<<“Enter the person ID to be searched : “;
cin>>id;
int ans;
ans = search (per, id);
if (ans == 1)
cout<<“The ID you searched for is present”;
else cout<<“The ID you searched for is not present”;
break;
case 3: arrange (per);
break;
default : cout<<endl<<“You entered an incorrect choice”<<endl;
}
getch();
}

/ Output */

Program to manage person data

Enter data for person 1
Enter person ID : 42
Enter person name : Arthur Dent
Enter person address : 42, Guide Road, New Delhi 110042
Enter person age : 42
Enter person nationality : Indian

Operation Menu

1. Print record of all people allowed to vote in India
2. Search for a record using ID
3. Arrange all records in descending order of ID
Enter your choice : 1

Details of people allowed to vote in India

Details for person number 1 eligible to vote
Person ID : 42
Person name : Arthur Dent
Person address : 42, Guide Road, New Delhi 110042
Person age : 42
Person nationality : Indian

Operation Menu

1. Print record of all people allowed to vote in India
2. Search for a record using ID
3. Arrange all records in descending order of ID
Enter your choice : 2

Enter the person ID to be searched : 42
The ID you searched for is present

Operation Menu

1. Print record of all people allowed to vote in India
2. Search for a record using ID
3. Arrange all records in descending order of ID
Enter your choice : 3

Person data in descending order of Person ID

Data for person 1

Person ID : 42
Person name : Arthur Dent
Person address : 42, Guide Road, New Delhi 110042
Person age : 42

Person nationality : Indian

Data for person 2

Person ID : 41
Person name : Douglas Adams
Person address : 41, Guide Road, New Delhi 110042
Person age : 42
Person nationality : Indian

One reply on “Structures – Person Data”

if ((p[i].age >= 18) && (strcmp (p[i].nat, “Indian”) == 0))

There was an error at this point earlier. It wrongly had a strcmp comparing the value to 1. This is the corrected code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.