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

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?

This article has been dead for over six months. Start a new discussion instead.