我认为我的代码很完美,但输出不正确

时间:2016-04-01 17:55:41

标签: c++ oop

这是我的代码:

#include <iostream>
#include <string>

using namespace std;

class Personal_Record {

    public:
        Personal_Record();
        Personal_Record(string nam, string dob, string addr, int mobNum, string ema, string hob);
        void Get_PersonalRecord();
        void Display_PersonalRecord();

    protected:
        string name;
        string dateOfBirth;
        string address;
        int mobileNumber;
        string emailId;
        string hobby;

};

Personal_Record::Personal_Record() 
{
    name = "";
    dateOfBirth = "";
    address = "";
    hobby = "";
}

Personal_Record::Personal_Record(string nam, string dob, string addr, int mobNum, string ema, string hob)
{
    name = nam;
    dateOfBirth = dob;
    address = addr;
    mobileNumber = mobNum;
    emailId = ema;
    hobby = hob;
}


void Personal_Record::Get_PersonalRecord()
{
    cout << endl << "Enter the name of the person: ";
    cin >> name;
    cout << endl << "Enter the date of birth: ";
    cin >> dateOfBirth;
    cout << endl << "Enter the address: ";
    cin >> address;
    cout << endl << "Enter the mobile number: ";
    cin >> mobileNumber;
    cout << endl << "Enter the e-mail id: ";
    cin >> emailId;
    cout << endl << "Enter any hobby the person has: ";
    cin >> hobby;
}

void Personal_Record::Display_PersonalRecord()
{
    cout << "Personal Record:" << endl << endl;
    cout << "1.Name: " << name << endl;
    cout << "2.Date Of Birth: " << dateOfBirth << endl;
    cout << "3.Address: " << address << endl;
    cout << "4.Mobile Number: " << mobileNumber << endl;
    cout << "5.E-mail Id: " << emailId << endl;
    cout << "6.Hobby" << hobby << endl; 
}

class Professional_Record {

    public:
        Professional_Record();
        Professional_Record(string nameOfCom, string pos, int xp);
        void Get_Professional_Record();
        void Display_Professional_Record();

    protected:
        string nameOfCompany;
        string position;
        int experienceInYears;

};

Professional_Record::Professional_Record()
{
    nameOfCompany = "";
    position = "";
    experienceInYears = 0;
}

Professional_Record::Professional_Record(string nameOfCom, string pos, int xp)
{
    nameOfCompany = nameOfCom;
    position = pos;
    experienceInYears = xp;
}

void Professional_Record::Get_Professional_Record()
{
    cout << endl << "Enter name of the company: ";
    cin >> nameOfCompany;
    cout << endl << "Enter position in this company: ";
    cin >> position;
    cout << endl << "Enter number of years of experience: ";
    cin >> experienceInYears;
}


void Professional_Record::Display_Professional_Record()
{
    cout << "Professional Record: " << endl << endl;
    cout << "Name of the Company: " << nameOfCompany << endl;
    cout << "Position in this company: " << position << endl;
    cout << "Number of years of experience: " << experienceInYears << endl; 
}

class Academic_Record {

    public:
        Academic_Record();
        Academic_Record(string nameOfCou, string nameOfCol, int passOut, float percent, string special);
        void Get_Academic_Record();
        void Display_Academic_Record();

    protected:
        string nameOfCourse;
        string nameOfCollege;
        int passOutYear;
        float percentage;
        string specialization;

};

Academic_Record::Academic_Record()
{
    nameOfCourse = "";
    nameOfCollege = "";
    passOutYear = 0;
    percentage = 0.0;
    specialization = "";
}

Academic_Record::Academic_Record(string nameOfCou, string nameOfCol, int passOut, float percent, string special)
{
    nameOfCourse = nameOfCou;
    nameOfCollege = nameOfCol;
    passOutYear = passOut;
    percentage = percent;
    specialization = special;
}

void Academic_Record::Get_Academic_Record()
{
    cout << endl << "Enter the name of the course: ";
    cin >> nameOfCourse;
    cout << endl << "Enter the name of the college: ";
    cin >> nameOfCollege;
    cout << endl << "Enter year of passout: ";
    cin >> passOutYear;
    cout << endl << "Enter the percentage: ";
    cin >> percentage;
    cout << endl << "Enter the subject the person has specialized in: ";
    cin >> specialization;
}

void Academic_Record::Display_Academic_Record()
{
    cout << endl << "Academic Details:" << endl;
    cout << "Name of the Course: " << nameOfCourse << endl;
    cout << "Name of the College: " <<  nameOfCollege << endl;
    cout << "Year of passout: " <<  passOutYear << endl;
    cout << "Percentage acquired: " << percentage << endl;
    cout << "The person has specialized in: " << specialization << endl;
}

class Bio_Data : public Personal_Record, public Professional_Record, public Academic_Record {

    public:
        void Display_BioData();

};

void Bio_Data::Display_BioData()
{
    Display_PersonalRecord();
    Display_Professional_Record();
    Display_Academic_Record();
}

int main()
{
    Bio_Data bd;
    cout << "Enter Personal Information: " << endl << endl;
    bd.Get_PersonalRecord();
    cout << "Enter Professional Information: " << endl << endl;
    bd.Get_Professional_Record();
    cout << "Enter Academic Information: " << endl << endl;
    bd.Get_Academic_Record();
    bd.Display_BioData();
    return 0;
}

输出以下代码: 输入个人信息:

输入此人的姓名:Suraj

输入出生日期:02/04/1996

输入地址:Varnam,A / 38,Pune

输入手机号码:8552004340

输入电子邮件ID: 输入此人的所有爱好:输入专业信息:

输入公司名称: 在此公司输入职位: 输入经验年数:输入学术信息:

输入课程名称: 输入学院的名称: 输入逾期年份: 输入百分比: 输入此专业人士的主题:个人记录:

1.名称:Suraj 2.出生日期:02/04/1996 3.地址:Varnam,A / 38,Pune 4.Mobile号码:2147483647 5.电子邮件ID: 6.Hobby 专业记录:

公司名称: 在这家公司的位置: 经验年数:0

学术细节: 课程名称: 学院名称: 密克年份:0 获得的百分比:0 该人专门从事:

这里的问题是,在拿走手机号码后,它没有给我提示电子邮件ID或输入任何其他字段。它只是给我输出。甚至在输出中移动号码。变成了一些垃圾价值而不是原来的没有。我进去了。我真的不知道发生了什么事。可以有人帮助我!

我已经在ubuntu和c ++ shell上运行了代码,他们似乎都给了我相同的结果。

我不知道这个问题叫什么,所以不要因为问这个问题而生我的气!

1 个答案:

答案 0 :(得分:3)

您将手机号码存储为int,其最大尺寸为32767 http://www.cplusplus.com/reference/climits/

8552004340太大了!

最好将其存储为字符串。