passing structure to functions

Hello, you have a nice thing going on here. Here is the exercise:

Create an address book program. the user should be able to not just fill out a single structure, but should be able to add new entries, each with a separate
name and phone number. Let the user add as many entries as he or she wants—is this easy to do? It is even possible? Add the ability to display all, or some of the entries, letting the user browse the list of entries.

The code is obviously not well written. Im into c++ for just a week now.

1)when the new_entry() function executes for every 1st time it doesn't let me type the name, like its getting some previous cin data.
2)Im not sure if im passing and returning the structure entries[] in function new_entry() correctly.

Thanks in advance.


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

using namespace std;

struct info
{
    string name;
    string phoneNo;
};

info new_entry(info entries[],int size)
{
    cout<<"\t\tNew Entry\n\nType Name :";
    getline(cin,entries[size].name,'\n');
    cout<<"Type Phone number :";
    getline(cin,entries[size].phoneNo,'\n');
    return entries[size];//is this correct?
}

void display_all(info entries[],int size)
{
    cout<<"\t\tlist of "<<size+1<<" entries\n\n";
    for (int i=0;i<size;i++)
    {
        cout<<i+1<<")\tName :"<<entries[i].name<<"\n";
        cout<<"\tPhone number"<<entries[i].phoneNo<<"\n\n";
    }
    cout<<"\t\tEnd of list...\n";
}

void display_selected(info entries[],int entry_no)
{
    cout<<"Entry No "<<entry_no+1<<": ";
    cout<<"\tName :"<<entries[entry_no].name<<"\n";
    cout<<"\t\tPhone number :"<<entries[entry_no].phoneNo<<"\n\n";
}

int main()
{
    int size=-1;
    info entries[1000];
    int select;
    int entry_no;

    cout<<"\t----Address book empty----------\n\nPress \t1 for new entry (maximum 1000)\n\t4 to quit :\n\n";
    cin>>select;

    while(select!=4)
    {
        if(select==1)
        {
            ++size;
            new_entry(entries,size);
        }
        else cout<<"Please type '1' or '4' :\n";

        if(size>=0)
        {
            cout<<"Type\t'1'for new entry\n\t'2' to display all entries\n\t'3' to select which entry to see\n\t'4' to quit :";
            cin>>select;
            if(select==1){}
            else if(select==2)
            {
                display_all(entries,size+1);
            }
            else if (select==3)
            {
                cout<<"Type Entry No to see it ("<<size+1<<") available";
                cin>>entry_no;
                display_selected(entries,entry_no-1);
            }
        }



    }
    cout<<"\t\tBYE!!!";
}
Topic archived. No new replies allowed.