Program has junk values even after input?

hello!
so I was working on a contacts manager code, and it's working fine except that,
my cmd stops working after I input 3rd contact and it's details, and the next error i'm getting is, when I input the contact details and display it, it's displaying random characters beside it! please help me, i'm new to c++
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
  #include<iostream>
#include<conio.h>
#include<string>
using namespace std;

class Contact
{
 private:

  char Name[50],Num[10];

 public:
  friend istream& operator >> (istream &in, Contact &str)
  {   cout<<"\nenter contact information\n"<<endl;
      in>>str.Name>>str.Num;
      return in;
  }

  friend ostream& operator << (ostream &out, Contact &str)
  {
      cout<<"\ncontact details are:\n";
      out<<str.Name<<"\n"<<str.Num<<endl;
      return out;
  }
 public:
     char* GetName(Contact obj)
     {
         return obj.Name;
     }
     void DisplayContact(Contact obj)
     {
         cout<<obj;
     }
};



class AddressBook
{
 private:

  static int n;

  Contact Rec[];

 public:

 void Add()
 {
     cout<<" -------------------------------------\n";
     cout<<"|      enter contact details          |\n";
     cout<<" -------------------------------------\n";
     cin>>Rec[n];
 }
 void Choice()
 {
     char c;
     for(;;)
     {
         cout<<"---------------------------------------------------\nMenu:\n"<<endl;
         cout<<"1. Add new Contact\n\n2. Display all Contacts\n\n3. Find contact\n\n4. Exit\n\n\n";
         cin>>c;
         switch(c)
         {
           case '1': n++;
                     Add();
                     break;
           case '2': ListAll();
                     break;
           case '3': FindContact();
                     break;
           case '4': exit(0);
         }
     }

}

 void FindContact()
 {
     char Name[20];
     cout<<"\nenter the name of the contact to find\n";
     cin>>Name;
     for(int i=0;i<=n;i++)
     {
         if(strcmp(Name,Rec[i].GetName(Rec[i]))==0)
         {
             cout<<"Contact Found\n";
             Rec[i].DisplayContact(Rec[i]);
             break;
         }
         else
         {
             cout<<"\nContact Not Found!!!\n\n";
             break;
         }
     }
 }
 void ListAll()
 {
    cout<<" -------------------------------------\n";
    cout<<"|      contact details are:           |\n";
    cout<<" -------------------------------------\n";
    for(int i=0;i<=n;i++)
    {
        Rec[i].DisplayContact(Rec[i]);
    }
 }

};
int AddressBook::n=0;
int main()
{
    AddressBook a;
    a.Choice();
}
I thnk you need to specify size of Contact rec[] and array size cannot be change so adding new element to array when array full is almost imposible, i think. you can use dynamic memory allocation to change its size but the value wiill be removed because it'll change the address of the array. You can use std::vector its easier
For that junk character, no idea sorry
I tried the vector method! Cmd stops working even after that!
Topic archived. No new replies allowed.