无法计算作为输入的位数 [英] Unable to Count Number of Digits as the Input
问题描述
我的程序:从用户那里获取有关个人详细信息的输入,然后输出信息.
My Program: To get input from the user about personal details and then output the information.
我的问题:程序无法计算数字作为电话号码字段输入的数量.它接受 10 位以上的数字作为电话号码的输入.
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.
我的目标:检查电话号码"的输入并确保该号码为 10 位数字.
My Goal: To check the input for 'Phone Number' and make sure that the number is 10-digits.
我的代码:
#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;
}
请您查看并检查为什么我无法输入真实的 10 位电话号码?我是 C++ 的新手,所以我无法找出解决方案.感谢您的帮助!
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!
推荐答案
您将电话号码存储为 int,但整数范围是 –2,147,483,648 到 2,147,483,647;输入超出该范围的 10 位数字会导致溢出,并可能导致程序崩溃.
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.
您可能希望将电话号码作为字符串读入,检查每个字符的有效性,然后根据需要对其进行处理(例如:区号变量、7 位数字变量、拒绝超出范围的输入并重新提示).
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).
这篇关于无法计算作为输入的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!