I'm creating a program using strings to create a contact card for a customer. It works to allow users to input information up until the "state" input part. It skips over this and doesn't allow a state to be entered and then continues with the remaining two inputs. On the display, it only displays the initials, name and street. It skips over the phone number. I know something must be wrong in the state/phone aspects of the program, but I'm confused as to what they are because I coded each input the same, but they aren't functioning the same.

#include <iostream>
#include <string>
using namespace std;

//function prototypes
string firstName = " ";
string lastName = " ";
string firstInitial = " ";
string lastInitial = " ";
string streetAddress = " ";
string city = " ";
string state = " ";
string zip = " ";
string phone = " ";

int main ()
{

    cout <<"Enter the customer's first name: ";
    cin >> firstName;
    cout <<"Enter the customer's last name: ";
    cin >> lastName;
    cout <<"Enter the customer's street address: ";
    getline (cin, streetAddress, '\n');
    cout <<"Enter the customer's city: ";
    getline (cin, city, '\n');
    cout <<"Enter the customer's state: ";
    cin >> state;
    cout <<"Enter the customer's zip code: ";
    cin >> zip;
    cout <<"Enter the customer's phone number: ";
    getline (cin, phone, '\n');

    firstInitial = firstName.substr (0,1);
    lastInitial = lastName.substr (0,1);


    //display output items
    cout << "This is how " << firstName << " " << lastName << "'s contact card will appear.";
    cout <<"\n";
    cout <<"****************************************"<< endl;
    cout <<"**********         " << firstInitial << lastInitial << "         **********"<<endl;
    cout <<"****************************************"<< endl;
    cout <<"**********         " << firstName << " " << lastName <<endl;
    cout <<"**********         " << streetAddress << endl;
    cout <<"**********         " << city << ", "<< state << " " << zip << endl;
    cout <<"**********         " << phone << endl;
    cout <<"****************************************"<< endl;

return 0;
} //end of main function

Recommended Answers

All 2 Replies

I figured out the first problem. I had to switch the state and zip inputs to getline as well. Now my only problem is with the output. The zipcode is going on to a different line, and the phone number isn't appearing at all. Any ideas about those?

Whenever you mix getline and cin, these kinds of problems show up. You say you've revised the code, but it sounds like you still have at least a few of the same types of problems? They sound like the mixing getline and cin problem. See this thread.

http://www.daniweb.com/software-development/cpp/threads/90228/flushing-the-input-stream

cin leaves a pesky '\n' in the stream and then getline gobbles it up and moves right along thinking its job is done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.