无法计数作为输入的位数
问题描述:
我的程序:从用户那里获得有关个人详细信息的输入信息,然后输出信息。无法计数作为输入的位数
我的问题:程序无法计算作为电话号码字段输入的位数。它接受超过10位数字作为电话号码的输入。
我的目标:检查“电话号码”的输入并确保该号码是10位数字。
我的代码:
#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++的新手,所以我无法找出解决方案。谢谢你的帮助!
答
您将电话号码存储为int,但整数范围是-2,147,483,648至2,147,483,647;在该范围外输入一个10位数字会导致溢出,并可能导致程序崩溃。
您可能希望以电话号码的形式读入字符串,检查每个字符的有效性,然后根据需要对其进行处理(例如:区号的变量,7位数的变量,拒绝超出范围输入并重新提示)。
您是否已经使用调试程序逐行传递程序?你注意到了什么? – 2014-09-19 02:06:39
@πάνταῥεῖ:调试器发现没有错误。我只是无法输入一个10位数的电话号码。 9位数字没问题。但是当输入10位数字时,程序意外关闭。 – Smith 2014-09-19 02:08:53
int的范围是-2,147,483,648到\t 2,147,483,647也许你比那更高,它的数字是10位但是有限 – JRowan 2014-09-19 02:15:36