1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
using namespace std;
struct contact
{
string name;
int days;
};
void sort(contact array[],int array_size);
contact* grow_array(contact* dummy_array,int* p_array_size)
{
contact* temp_array=new contact[*p_array_size*2];
for(int i=0;i<*p_array_size;i++)
{
temp_array[i].name=dummy_array[i].name;
temp_array[i].days=dummy_array[i].days;
}
delete[] dummy_array;
*p_array_size*=2; ///updating the array_size.
return temp_array;
}
void add_contact(contact *dummy_array,int *p_x,int array_size)
{
cout<<"Enter name: ";
cin>>dummy_array[*p_x].name;
do
{
cout<<"Enter number of days you last met "<<dummy_array[*p_x].name<<": ";
cin>>dummy_array[*p_x].days;
}while(dummy_array[*p_x].days<0);
(*p_x)++;
sort(dummy_array,array_size);
}
void update_contact(contact array[],int array_size)
{
int i=0;
string search_name;
cout<<"Enter name whose info you would like to update(case sensitive): ";
cin>>search_name;
for(i=0;search_name!=array[i].name&&i<array_size;i++)
{
if(search_name==array[i].name)///condition of array_size already included in for function above
{
cout<<"Found "<<array[i].name<<". Last met "<<array[i].days<<" ago! updated number of days last met: ";
cin>>array[i].days;
}
else if(i=(array_size-1)&&array[i].name!=search_name)
{
cout<<search_name<<" not found in database!";
}
}
}
void display_spec_contact(contact array[], int array_size)
{
int i=0;
string search_name;
cout<<"Enter name whose info you would like to see(case sensitive): ";
cin>>search_name;
for(i=0;array[i].name!=search_name&&i<array_size;i++)
{
if(search_name==array[i].name)///condition of array_size already included in for function above
{
cout<<"Name: "<<array[i].name<<"\nLast met: "<<array[i].days<<" ago";
}
else if(i=(array_size)-1&&search_name!=array[i].name)
{
cout<<search_name<<" not found in database!";
}
}
}
void display_all_contact(contact array[],int array_size)
{
for(int i=0;i<array_size;i++) ///displays contact till the end of array size
{
cout<<"Name: "<<array[i].name<<endl<<"Last met: "<<array[i].days<<endl<<endl;
}
}
void sort(contact array[], int array_size)
{
contact temp;
for(int i=0;i<array_size;i++)///till end of array_size
{
int i_of_smallest_days=i; /// i_of_smallest days = meaning = index_of_smallest_days
for(int j=(i++);j<array_size;j++) ///only 5 for now.. need to update
{
if(array[j].days<array[i_of_smallest_days].days)
{
i_of_smallest_days=j;
}
}
temp.name=array[i_of_smallest_days].name;
temp.days=array[i_of_smallest_days].days;
array[i].name=array[i_of_smallest_days].name;
array[i].days=array[i_of_smallest_days].days;
array[i_of_smallest_days].name=temp.name;
array[i_of_smallest_days].days=temp.days;
}
}
int main()
{
int x=0; ///to update the position array upto where position is filled.
int array_size=5;
string choice;
contact* array=new contact[array_size];
do
{
cout<<endl<<endl;
cout<<"A to add contact\t U to update contact\t Da to display all contact\nE to exit\tDs to display info of specific contact\nEnter what would you like to do: ";
cin>>choice;
if(array_size-1==x) ///to update size of dynamically created array
{
array=grow_array(array,&array_size);
}
if(choice=="A")
{
add_contact(array,&x,array_size);
}
else if(choice=="U")
{
update_contact(array,array_size);
}
else if(choice=="Da")
{
display_all_contact(array,array_size);
}
else if(choice=="Ds")
{
display_spec_contact(array,array_size);
}
else if(choice=="E")
{
cout<<"Goodbye!\n";
}
else
{
cout<<"Wrong choice!";
}
}while(choice!="E");
}
|