(Ughhh! so many problems) Hi guys! The problem is in that the last couple of different programs I wrote, I got continuous message of "(program_name).exe has stopped working". I am not sure but i think this is what is called 'program crash'. i think it could be because of 'memory leak', but i think i made sure to delete dynamic memories wherever possible.
But even if I didn't delete memory, for such a small program in C++, i don't think system should have run out of memory! I have 4 GB RAM. Also, using c++ and windows 8.
Any help(either with memory leak in code, solving 'program stopped working' problem or improving code) would be highly appreciated. And help guys, I am stuck with this program since noon! There's isn't just a specific problem so i am just posting the entire code.
The question from jumping into c++-pointers chapter was:
Write a program that lets users keep track of the last time they talked to each of their friends.Users should be able to add new friends (as many as they want!) and store the number of days ago that they last talked to each friend. Let users update this value (but don't let them put in bogus numbers like negative values). Make it possible to display the list sorted by the names of
the friends of by how recently it was since they talked to each friend.
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
|
#include <iostream>
using namespace std;
struct contact
{
string name;
int days;
};
void sort(contact array[]);
void add_contact(contact *dummy_array,int *p_x)
{
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); //to prevent input of negative numbers;
(*p_x)++;
}
void update_contact(contact array[])
{
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++)
{
}
cout<<"Found "<<array[i].name<<". Last met "<<array[i].days<<" ago! updated number of days last met: ";
cin>>array[i].days;
}
void display_spec_contact(contact array[])
{
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++)
{
}
cout<<"Name: "<<array[i].name<<"\nLast met: "<<array[i].days<<" ago";
}
void display_all_contact(contact array[])
{
sort(array);
for(int i=0;i<5;i++) ///for now just 5. change when include option of increasing array size
{
cout<<"Name: "<<array[i].name<<endl<<"Last met: "<<array[i].days<<endl<<endl;
}
}
void sort(contact array[])
{
contact temp;
for(int i=0;i<5;i++)///only 5 for now. need to update
{
int i_of_smallest_days=i; /// i_of_smallest days = meaning = index_of_smallest_days
for(int j=(i++);j<5;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;//added at first to save from writing after each function
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\n\nEnter what would you like to do: ";
cin>>choice;
if(array_size-1==x) ///to update size of dynamically created array
{
contact *temp_array=new contact[array_size*2];
for(int i=0;i<array_size;i++)
{
temp_array[i]=array[i];
}
delete[] array;
array=temp_array;
delete[] temp_array;
array_size*=2;
}
if(choice=="A")
{
add_contact(array,&x);
}
else if(choice=="U")
{
update_contact(array);
}
else if(choice=="Da")
{
display_all_contact(array);
}
else if(choice=="Ds")
{
display_spec_contact(array);
}
else if(choice=="E")
{
cout<<"Goodbye!\n";
}
else
{
cout<<"Wrong choice!";
}
}while(choice!="E");
}
|