首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >头文件的Cin和Cout

头文件的Cin和Cout
EN

Stack Overflow用户
提问于 2016-12-01 13:16:27
回答 1查看 3.2K关注 0票数 0

更新:

我从用户输入中得到信息进入构造函数,但是现在信息不想打印到void语句中,结果是空的.有什么帮助吗?

侧问题:能回答为什么当我为地址添加空格时,输入会流进下一个问题而跳过所述的问题吗?有没有办法防止输入流进下一个问题?

代码语言:javascript
复制
#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; 

}

演示头:

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

发布于 2016-12-01 17:01:48

移动线:

代码语言:javascript
复制
PatientDemographicInformation patient1(
    patientMedicalRecordNo, 
    patientFirstName,
    patientMiddleInitial,
    patientLastName,
    patientStreetAddress1,
    patientStreetAddress2,
    patientCity,
    patientState,
    patientZip5,
    patientZip4,
    patientHomeAreaCode,
    patientHomePhoneNo,
    patientGender,
    patientDateOfBirth
);

毕竟,您的cin调用就在您调用patient1.printPatientDemographicInformation();之前

这里要做的是将许多空字符串值传递给构造函数,构造函数会复制它们(顺便说一下,它可能会复制它们两次,考虑用const string&替换const string&类型参数)。然后请求输入,输入将只存储在您的局部变量中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40911664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档