My input all on the same line cin?

I have a weird issue I can't seem to solve I am not sure if I am overlooking something simple or if it is something else entirely.

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
126
127
128
129
130
131
132
133
  int main()
{
    // patients medical record number
    string patientMedicalRecordNo;
    // patient's first name
    string patientFirstName;
    // patient's middle initial
    char patientMiddleInitial;
    // patient's last name
    string patientLastName;
    // patient's address part 1
    string patientStreetAddress1;
    // patient's address part 2
    string patientStreetAddress2;
    // patient's city
    string patientCity;
    // patient's state
    string patientState;
    // patient's 5 digit zip code
    string patientZip5;
    // patient's 4 digit zip code
    string patientZip4;
    // patients home area code
    string patientHomeAreaCode;
    // patient's home phone number
    string patientHomePhoneNo;
    // patient's gender
    char patientGender;
    // patient's date of birth
    int patientDateOfBirth;
    // patient's health insurance code
    char patientInsuranceCode;
    // patient's policy number
    string patientPolicyNo;
    // patient's group number
    string patientGroupNo;
    // patient diagnosis code
    string patientDiagnosisCode;
    // patient monthly payment status
    bool patientMonthlyPaymentStatus;
    // patient monthly payment amount
    double patientMonthlyPaymentAmt;
    // charge to patient's account
    double patientCharge;
    // patient balance
    double patientBalance;
        
    PatientDemographicInformation yanovichDemo(patientMedicalRecordNo, patientFirstName, patientMiddleInitial, patientLastName, patientStreetAddress1, patientStreetAddress2, patientCity, patientState, patientZip5, patientZip4, patientHomeAreaCode, patientHomePhoneNo, patientGender, patientDateOfBirth);

    cout << "Enter the patient medical record number: ";
    cin  >> patientMedicalRecordNo;

    cout << "Enter the patient's first name: ";
    cin  >> patientFirstName;

    cout << "Enter the patient's middle initial: ";
    cin  >> patientMiddleInitial;

    cout << "Enter the patient's last name: ";
    cin  >> patientLastName;

    cout << "Enter the patient's street address (line 1): ";
    cin  >> patientStreetAddress1;

    cout << "Enter the patient's street address (line 2): ";
    cin  >> patientStreetAddress2;

    cout << "Enter the patient's city: ";
    cin  >> patientCity;

    cout << "Enter the patient's state: ";
    cin  >> patientState;

    cout << "Enter the patient's five digit zip code: ";
    cin  >> patientZip5;

    cout << "Enter the patient's four digit zip code: ";
    cin  >> patientZip4;

    cout << "Enter the patient's home area code: ";
    cin  >> patientHomeAreaCode;

    cout << "Enter the patient's home phone number: ";
    cin  >> patientHomePhoneNo;

    cout << "Enter the patient's gender (M = Male, F = Female): ";
    cin  >> patientGender;

    cout << "Enter the patient's date of birth (format MMDDYYYY): ";
    cin  >> patientDateOfBirth; 

    PatientInsuranceInformation yanovichInsurance(patientMedicalRecordNo, patientInsuranceCode, 
          patientPolicyNo, patientGroupNo);
      
    cout << "Enter the patient insurance code: ";
    cin  >> patientInsuranceCode;

    cout << "Enter the patient policy number: ";
    cin  >> patientPolicyNo;

    cout << "Enter the patient group number: ";
    cin  >> patientGroupNo;

    PatientBillingInformation yanovichBilling(patientMedicalRecordNo, patientDiagnosisCode, 
          patientMonthlyPaymentStatus, patientMonthlyPaymentAmt, patientCharge, patientBalance);

    cout << "Enter the patient diagnosis code: ";
    cin  >> patientDiagnosisCode;

    cout << "Enter the patient's monthly payment status (Y = Monthly Plan, N = No Monthly Plan): ";    
    do
    {
        cout << "Enter monthly payment amount: ";
        cin  >> patientMonthlyPaymentAmt;
    }
    while (patientMonthlyPaymentStatus == true);
    
    banner();
    
    cout << endl << endl;
     
    yanovichDemo.printPatientDemographicInformation( );

    cout << endl << endl ;

    yanovichInsurance.printPatientInsuranceInformation( );

    cout << endl << endl;

    yanovichBilling.printPatientBillingInformation( );
    
    return 0;
}


This outputs fine until patient address two where it does as followed,
Enter the patient's street address (line 2): Enter the patient's city: Enter the patient's state: Enter the patient's five digit zip code: Enter the patient's four digit zip code:. All on one line allowing only one entry for all those items. Any help would be appreciated. This only occurs when I have more than word and have to space them.
Last edited on
Hello rmeade,

It is not really a weird issue once you understand how things work.

The lines with "cin >> something" is known as formatted input which works best when "something" is a numeric variable. If you try to enter anything other than a number "cin" will fail and no other input will happen until the state bits are cleared. This also means that "cin" will extract from the input buffer up to the first white space it encounters or new line which ever comes first. Anything after the white space is left in the input buffer until the next line that would read from the input buffer.

So when you enter a name or address you will only get up to the first white space leaving whatever is left in the input buffer.

I would suggest using "std::getlline(std::cin, str);" where "str" is the string variables that you use. Any "std::getline" can be followed by "std::cin >>", but after the last "std::cin >>" and before the next "std::getline" you will need to use this line:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. This will clear the input buffer of anything that is left in the buffer.

This should clear up your problem.

Hope that helps,

Andy
Thank you for the help this solved my issue.
Topic archived. No new replies allowed.