Simple Address Book

Hello Everyone,

I'm a noob of sorts. I have one class of C++ under my belt but I'm brushing up on my skills for the second class using Alex Allain's Jumping into C++.

One of the exercises is to create a simple address book and somehow I've convoluted the code so that it skips the first entry that the user is prompted for. For example, the user is supposed to be prompted with:

Please enter the following information for your new contact:
First Name:


Instead they are prompted with:

Please enter the following information for your new contact:
First Name:
Last Name:


without the ability to fill in the first input. I'm pretty sure this has to do with the way I have used getline but can't seem to figure it out. Any help would be appreciated. Here is the code:

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

using namespace std;

void newEntry();

struct AddressBook
{
    string firstName;
    string lastName;
    string streetAddress;
    string city;
    string state;
    string zipCode;
    string phone;
};

void menu()
{
    int choice = 0;
    cout << "******Your Address Book******" << endl << endl;
    cout << "Menu:" << endl;
    cout << "1) New Entry" << endl;
    cout << "2) Display All Entries" << endl;
    cout << "3) Display Entries Starting with a Certain Letter" << endl;
    cout << "4) Exit" << endl << endl;
    cin >> choice;
    
    while (choice > 4 || choice < 1)
    {
        cout << "I'm sorry. That is an invalid choice. Please try again." << endl << endl;
        
        cout << "******Your Address Book******" << endl << endl;
        cout << "Menu:" << endl;
        cout << "1) New Entry" << endl;
        cout << "2) Display All Entries" << endl;
        cout << "3) Display Entries Starting with a Certain Letter" << endl;
        cout << "4) Exit" << endl << endl;
        cin >> choice;
    }
    
    if (choice == 1) {
        newEntry();
    }

}

void newEntry()
{
    AddressBook contact;
    cout << "Please enter the following information for your new contact:" << endl;
    cout << "First Name: " << endl;
    getline(cin, contact.firstName, '\n');
    cout << "Last Name: " << endl;
    getline(cin, contact.lastName, '\n');
    cout << "Street Address: " << endl;
    getline(cin, contact.streetAddress, '\n');
    cout << "City: " << endl;
    getline(cin, contact.city, '\n');
    cout << "State: " << endl;
    getline(cin, contact.state, '\n');
    cout << "Zip Code: " << endl;
    getline(cin, contact.zipCode, '\n');
    cout << "Phone Number: " << endl;
    getline(cin, contact.phone, '\n');
}

int main()
{
    menu();
    
}
The reason this is happening is because you are mixing cin/getline calls. When cin is called it leaves the carriage return in the buffer (when you press enter) and then the next getline call sees the buffer isn't empty and counts that as the input.

You will want to clear the buffer after your first cin call with something like this.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Awesome! That worked.

Thanks for explaining why as well. Very helpful.
Topic archived. No new replies allowed.