简体   繁体   English

头文件的Cin和Cout

[英]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? 我从用户输入中获取了要进入构造函数的信息,但是现在该信息不想打印到void语句中,而是显示为空...有帮助吗?

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(); 在所有cin调用之后,恰好在调用patient1.printPatientDemographicInformation();之前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& ). 您在这里所做的是将大量空字符串值传递给构造函数,该构造函数将其复制(顺便说一句,它可能会复制两次,请考虑使用const string&替换string类型参数)。 Then you ask for input which will be stored only in your local variables. 然后,您要求输入将仅存储在本地变量中的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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