簡體   English   中英

頭文件的Cin和Cout

[英]Cin and Cout for Header files

更新:

我從用戶輸入中獲取了要進入構造函數的信息,但是現在該信息不想打印到void語句中,而是顯示為空...有幫助嗎?

補充問題:有人可以回答為什么我在地址中加空格時輸入內容會滲入下一個問題,而會跳過該問題嗎? 有沒有辦法防止輸入流失到下一個問題?

#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; 

}

演示標題:

#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

移動線:

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

在所有cin調用之后,恰好在調用patient1.printPatientDemographicInformation();之前patient1.printPatientDemographicInformation();

您在這里所做的是將大量空字符串值傳遞給構造函數,該構造函數將其復制(順便說一句,它可能會復制兩次,請考慮使用const string&替換string類型參數)。 然后,您要求輸入將僅存儲在本地變量中的輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM