Adding an entry into array.

Hey guys!

My program is suppose to be as a virtual phone book that allows you to add,search, display names and numbers.

At the beginning you are able to add up to 10 entries and then from there the program goes to a menu where you can add more entries, search etc.

My problem is that I am unable to add an entry into the existing list of names/phone numbers.

Example: At the beginning I add Joe,Albert,Barry.
It sorts them into Albert, Barry, Joe (good so far!)

However, if I choose to add another entry (Carl) it becomes Barry,Carl,Joe.

The functions I am using to add entries are: GetEntries (for initial entries) and Addentries for more entries during the main program.

I would really appreciate if someone could help me figure out how I can get these two functions to work along side one another and display everything.

Thanks in advance!

*******************************COPY OF CODE**********************************

#include <iostream>
#include <string>

using namespace std;

int getEntries (string user[], string phone[])
{

int i=0;
int w=0;

do{
cout << "Enter name: ";
getline (cin, user[i]);

if(user[i] == "***END***")
break;

cout << "Enter number: ";
getline (cin, phone[i]);

i++;

}while ((i<10) && (user[i] != "***END***"));

return i;
}

int addEntries(string user[], string phone[])
{

int i = 0;

string totaluser,totalphone;
cout << "Enter the name: ";
cin.ignore();
getline (cin, user[i]);
cout << "Enter number: ";
getline (cin, phone[i]);

i++;

return i;
}



void sortBook (string names[],int entries, string phone[])
{
int first;
string temp;
string temp2;

for(int i=entries-1; i>0; i--)
{
first = 0;

for (int j=1; j<=i; j++)
{
if (names[j] > names[first])
{
first = j;
}

temp = names[first];
names[first] = names[i];
names[i] = temp;

temp2 = phone[first];
phone[first] = phone[i];
phone[i] = temp2;

}
}
}

void display ( string names[], int entries, string phone[])
{

for (int i=0; i<entries; i++)
cout << names[i] << " " << phone[i] << endl;
}

void searchArray (string names[], string phone[], int entries)
{
string key;
cout << "Who's number do you want?";
cin >> key;

for(int i=0; i<entries; i++)
{
if(names[i] == key)
cout << "The number is " << phone[i] << endl;
}
}

int main ()
{
int i;
string user[10];
string phone[10];
string choice;

i = getEntries (user, phone);


while (choice != "quit")
{
cout << "Enter menu choice (display, add, search, quit): ";
cin >> choice;

if(choice == "a")
{
addEntries(user,phone);

}

if(choice == "d")
{
sortBook (user,i, phone);
display (user,i, phone);
}



if(choice == "s")
{
searchArray(user,phone,i);
}


}

return 0;

}
When you add an entry, you're adding the new person to the 0th element of the array, so that's overwriting whatever data was there.

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
#include <iostream>
#include <string>

using namespace std;

int getEntries (string user[], string phone[])
{
    int i=0;
    int w=0;
    
    do{
        cout << "Enter name: ";
        getline (cin, user[i]);
        
        if(user[i] == "***END***")
        break;
        
        cout << "Enter number: ";
        getline (cin, phone[i]);
        
        i++;
        
    }while ((i<10) && (user[i] != "***END***"));
    
    return i;
}

int addEntries(string user[], string phone[])
{

    int i = 0;
    
    string totaluser,totalphone;
    cout << "Enter the name: ";
    cin.ignore();
    getline (cin, user[i]); //<-- i = 0 
    cout << "Enter number: ";
    getline (cin, phone[i]); //<-- i = 0
    
    i++;
    
    return i;
}

void sortBook (string names[],int entries, string phone[])
{
    int first;
    string temp;
    string temp2;
    
    for(int i=entries-1; i>0; i--)
    {
        first = 0;
        
        for (int j=1; j<=i; j++)
        {
            if (names[j] > names[first])
            {
                first = j;
            }
            
            temp = names[first];
            names[first] = names[i];
            names[i] = temp;
            
            temp2 = phone[first];
            phone[first] = phone[i];
            phone[i] = temp2;
        }
    }
}

void display ( string names[], int entries, string phone[])
{
    for (int i=0; i<entries; i++)
        cout << names[i] << " " << phone[i] << endl;
}

void searchArray (string names[], string phone[], int entries)
{
    string key;
    cout << "Who's number do you want?";
    cin >> key;
    
    for(int i=0; i<entries; i++)
    {
        if(names[i] == key)
            cout << "The number is " << phone[i] << endl;
    }
}

int main ()
{
    int i;
    string user[10];
    string phone[10];
    string choice;
    
    i = getEntries (user, phone);

    while (choice != "quit")
    {
        cout << "Enter menu choice (display, add, search, quit): ";
        cin >> choice;
        
        if(choice == "a")
        {
            addEntries(user,phone);
        }
        
        if(choice == "d")
        {
            sortBook (user,i, phone);
            display (user,i, phone);
        }
        
        if(choice == "s")
        {
            searchArray(user,phone,i);
        }
    }
    
    return 0;

}
Last edited on
Thanks for the quick reply!

Would I need to bring in the existing entries into the function?
Yes - you'd want to keep track of the number of entries already in the array when you go to add another one. If you already have the maximum number of entries, you don't want to go out of bounds on the array. And you want to save the updated value of entries after you add, so that if you call other functions from main, they have an accurate number of entries.
So I figure I would have to set 'i' to a new spot at the end of the array list. But I'm not quite sure exactly how I would do that.
You know the next index for data since you've saved it here.
i = getEntries (user, phone);

You're returning a value from the add function but not saving it anywhere. You could make this be an increment of i and save it back in main like you did with the get function. If i is already at 10 (or whatever the max size of the array is), then you could reject any attempt to add more entries.
Topic archived. No new replies allowed.