Unable to Count Number of Digits as the Input

倖福魔咒の 提交于 2021-02-10 12:25:10

问题


My Program: To get input from the user about personal details and then output the information.

My Problem: The program is unable to count the number of digits as the input for Phone Number field. It accepts more than 10-digits as an input for the phone number.

My Goal: To check the input for 'Phone Number' and make sure that the number is 10-digits.

My Code:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

class Personal_Details
{
public:
    void setFirstName   (string newFirstName);
    void setLastName    (string newLastName);
    void setBirthdate   (int newBirthday);
    void setEMail       (string newEMail);
    void setPhoneNumber (int newPhoneNumber);
    void QUESTIONS();
    string getFirstName();
    string getLastName();
    string getEMail();
    int getPhoneNumber();
    int    getBirthdate();

private:
    string FirstName;
    string LastName;
    string EMail;
    int PhoneNumber;
    int Birthdate;
};

void Personal_Details :: setFirstName (string newFirstName)
{
    FirstName = newFirstName;
}

void Personal_Details :: setLastName (string newLastName)
{
    LastName = newLastName;
}

void Personal_Details :: setBirthdate (int newBirthdate)
{
    Birthdate = newBirthdate;
}

void Personal_Details :: setEMail (string newEMail)
{
    EMail = newEMail;
}

void Personal_Details :: setPhoneNumber (int newPhoneNumber)
{
    PhoneNumber = newPhoneNumber;
}

string Personal_Details :: getFirstName()
{
    return FirstName;
}

string Personal_Details :: getLastName()
{
    return LastName;
}

int Personal_Details :: getBirthdate()
{
    return Birthdate;
}

string Personal_Details :: getEMail()
{
    return EMail;
}

int Personal_Details :: getPhoneNumber()
{
    if (PhoneNumber>12)
    {
        cout <<"INVALID. Program will now close.";
    }
    system ("PAUSE");

    return PhoneNumber;
}

void Personal_Details :: QUESTIONS()
{
    cout << "A.) Enter the following details:-" << endl <<endl;
    cout << "First Name: ";
    getline (cin, FirstName);
    cout << endl;

    cout << "Last (Family) Name: ";
    getline (cin, LastName);
    cout << endl;

    cout << "Birthdate: ";
    cin >> Birthdate;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << endl;

    cout << "Email Address: ";
    getline (cin, EMail);
    cout << endl;

    cout << "Phone Number: ";
    cin >> PhoneNumber;
    cout << endl;

    cout<< FirstName <<" "<< LastName << endl 
        <<"Birthdate: "<< Birthdate << endl 
        <<"Email ID: "<< EMail << endl 
        <<"Phone Number: "<< PhoneNumber << endl;
}

int main()
{
    Personal_Details NewContact;
    cout << "- Add New Contact Information -" << endl << endl; 
    cout << "Note: Please type in only intergers (numbers) for 'Birthdate' and 'Phone Number' field. Also, phone number should be less than/or equal to 10 digits." << endl;
    cout << "Else, this program will terminate abruptly." << endl << endl;
    NewContact.QUESTIONS();
    cout << endl << endl;

    cout << "Press Any Key to Exit.";
    cin.ignore();
    cin.get();

    return 0;
}

Please can you review and check why I am not able to input a real 10-digit phone number? I am new to C++, so I am unable to figure out a solution. Thanks for your help!


回答1:


You are storing the phone number as an int, but the range of integers is –2,147,483,648 to 2,147,483,647; entering a 10 digit number outside of that range results in overflow and may be what is crashing your program.

You may want to read in the phone number as a string, check each character for validity, and process it however you need to (e.g.: a variable for area code, a variable of 7 digit number, reject out of range input and re-prompt).



来源:https://stackoverflow.com/questions/25925241/unable-to-count-number-of-digits-as-the-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!