简体   繁体   中英

Cin and Cout for Header files

UPDATE:

I got the info from the user input to go into the constructor, but now the info does not want to print into the void statements and comes out empty... Any help?

Side Question: Can anyone answer as to why when I put spaces for the address the input will bleed over into the next question and will skip said question? Is there a way to prevent the input to bleed into the next question?

#include "PTDemo.h"
#include <iostream>

using namespace std;


int main ()

{      
    string patientMedicalRecordNo;               
    string patientFirstName;            
    char   patientMiddleInitial;                
    string patientLastName;             
    string patientStreetAddress1;                 
    string patientStreetAddress2;               
    string patientCity;                             
    string patientState;                
    string patientZip5;         
    string patientZip4;             
    string patientHomeAreaCode;         
    string patientHomePhoneNo;          
    char   patientGender;           
    int    patientDateOfBirth;  

    PatientDemographicInformation patient1(patientMedicalRecordNo, patientFirstName, 
                                          patientMiddleInitial, patientLastName, 
                                          patientStreetAddress1, patientStreetAddress2, 
                                          patientCity, patientState, patientZip5, 
                                          patientZip4, patientHomeAreaCode, 
                                          patientHomePhoneNo, patientGender, patientDateOfBirth);


        cout << "Enter the patient's 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;

        patient1.printPatientDemographicInformation();

        return 0; 

}

Demo Header:

#ifndef PATIENT_DEMOGRAPHIC_INFORMATION
#define PATIENT_DEMOGRAPHIC_INFORMATION

//system defined preprocessor statement for input/output operations.
#include <iostream>
// system defined preprocessor statement for setprecision operations. 
#include <iomanip> 

#include <algorithm>

#include <ctime>

using namespace std; 

class PatientDemographicInformation
{
    private:
    string patientMedicalRecordNo;                  
    string patientFirstName;            
    char   patientMiddleInitial;                    
    string patientLastName;             
    string patientStreetAddress1;                   
    string patientStreetAddress2;                
    string patientCity;                            
    string patientState;                
    string patientZip5;             
    string patientZip4;             
    string patientHomeAreaCode;         
    string patientHomePhoneNo;          
    char   patientGender;               
    int    patientDateOfBirth;          

    public:
    // The constructor is passed arguments 
    PatientDemographicInformation(string medicalRecordNo, string firstName, 
                                  char middleInitial, string lastName, 
                                  string streetAddress1, string streetAddress2, 
                                  string city, string state, string zip5, 
                                  string zip4, string homeAreaCode, string 
                                  homePhoneNo, char gender, int dateOfBirth);

    // Returns the patient’s medical record number.
    string getPatientMedicalRecordNo( );
    // Returns the patient’s first name a space middle initial a space then the last name. 
    string getPatientName( );
    // Returns the patient’s state in all capital letters.
    string getPatientState( );
    // Prints the patient’s street address line 1, then on the next line street address line 2, on the 
    // next line city, a comma “,” then a space and the state (ALL CAPITAL LETTERS), then 
    // two spaces zip-5, then a dash “-”, then zip-4. 
    void printPatientAddress( );
    // Returns the patient’s home area code enclosed in parenthesis, then the home phone number with a dash 
    // “-” between the exchange and the number.
    string getPatientPhoneNumber( );
    // Returns the patient’s gender description. 
    char getPatientGender( );
    // Prints the patient’s date of birth with dashes. 
    void printPatientDateOfBirth( );
    // Returns the patient’s age. 
    int getPatientAge( );
    // Prints the patient demographic information.
    void printPatientDemographicInformation( );
};
    // 
    PatientDemographicInformation::PatientDemographicInformation (string medicalRecordNo, string firstName, 
                                                                      char middleInitial, string lastName, 
                                                                      string streetAddress1, string streetAddress2, 
                                                                      string city, string state, string zip5, 
                                                                      string zip4, string homeAreaCode, string 
                                                                      homePhoneNo, char gender, int dateOfBirth)
    {
        patientMedicalRecordNo = medicalRecordNo; 
        patientFirstName = firstName; 
        patientMiddleInitial = middleInitial;
        patientLastName = lastName;
        patientStreetAddress1 = streetAddress1;
        patientStreetAddress2 = streetAddress2; 
        patientCity = city;
        patientState = state; 
        patientZip5 = zip5;
        patientZip4 = zip4;
        patientHomeAreaCode = homeAreaCode;
        patientHomePhoneNo = homePhoneNo;
        patientGender = gender;
        patientDateOfBirth = dateOfBirth; 
    }
    string PatientDemographicInformation::getPatientMedicalRecordNo( )
    {
                return patientMedicalRecordNo; 
    }

    string PatientDemographicInformation::getPatientName( )
    {
        return patientFirstName + " " + patientMiddleInitial + " " + patientLastName; 
    }

    string PatientDemographicInformation::getPatientState( )
        {       
                std::transform(patientState.begin(), patientState.end(),patientState.begin(), ::toupper);
                return patientState; 
    }

    void PatientDemographicInformation::printPatientAddress(void)
    {
        cout << patientStreetAddress1 << endl;
        cout << "                    " <<  patientStreetAddress2 << endl;
        cout << "                    " 
                     <<  patientCity  << ", "  
                     << getPatientState() << "  " 
                     << patientZip5  << "-" 
                     << patientZip4           << endl;      

    }

    string PatientDemographicInformation::getPatientPhoneNumber( )
    {
            if (patientHomePhoneNo != " ")
            {
            return "(" + patientHomeAreaCode + ")" + patientHomePhoneNo.substr(0,3) + "-" + patientHomePhoneNo.substr(3,6); 
            }

    }

    char PatientDemographicInformation::getPatientGender( )
    {
            if (patientGender == 'F' || patientGender == 'f')
                {
        return patientGender; 
                }
            else if (patientGender == 'M' || patientGender == 'm')
                {
                return patientGender; 
                }
            else 
            {
                cout << "That is not a valid gender input ";
            }
    }

    void PatientDemographicInformation::printPatientDateOfBirth(void)
    {
            if (patientDateOfBirth > 0)
            {
            int day   =  (patientDateOfBirth / 1000000);
            int month =  ((patientDateOfBirth % 1000000) / 10000);
            int year  =  (patientDateOfBirth % 10000); 

            cout << day << "/" << month << "/" << year; 
            }
    }

    int PatientDemographicInformation::getPatientAge( )
    {  
           time_t t = time(0);   // get time now
           struct tm * now = localtime( & t );
           int yearNow = (now->tm_year + 1900);
           int birthYear  =  (patientDateOfBirth % 10000); 
           return yearNow - birthYear; 

    }

    void PatientDemographicInformation::printPatientDemographicInformation( )
    {
                cout << "- - - - - - - - - - - - - - - - - - - - - PATIENT INFORMATION - - - - - - - - - - - - - - - - - - - -"<< endl; 
        cout << "Medical Record NO.: "     << getPatientMedicalRecordNo()                << endl;
        cout << "     Patients Name: "     << getPatientName()                           << endl;
                cout << "           Address: ";
                printPatientAddress(); 
                cout << "                    "     << getPatientPhoneNumber()       << endl;                                                                       
        cout << "Gender: "                 << getPatientGender()            << " "                       
                     << "Date of Birth: "; 
                printPatientDateOfBirth(); 
                cout << " "        
                     << "Age: "                    << getPatientAge()                            << endl; 
                cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"<< endl <<endl;
    }

#endif

Move the line:

PatientDemographicInformation patient1(
    patientMedicalRecordNo, 
    patientFirstName,
    patientMiddleInitial,
    patientLastName,
    patientStreetAddress1,
    patientStreetAddress2,
    patientCity,
    patientState,
    patientZip5,
    patientZip4,
    patientHomeAreaCode,
    patientHomePhoneNo,
    patientGender,
    patientDateOfBirth
);

After all your cin calls just before you call patient1.printPatientDemographicInformation();

What you are doing here is you pass a lot of empty string values to the constructor, which copies them (btw it might copy them twice, consider replacing string type parameters with const string& ). Then you ask for input which will be stored only in your local variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM