C++输入超过 11 个整数时程序终止

C++ program terminate when entering more than 11 integer

本文关键字:整数 程序 终止 输入 C++      更新时间:2023-10-16

我有一个用C++编写的程序,要求用户输入他/她的 11 位数字 手机号码。但是当我输入 11 位数字时,程序continue没有 执行成功代码。

 string FriendName, FriendAdd,  EmailAdd;
 long MobileNumber, counter, counter1, counter2;
 //Create a new structure
 struct personData {
   string namePerson, addressPerson, emailAddress;
   long age;
   struct personData *next;
 };
//Initialize pointers to personData structure
typedef struct personData node;
node *firstRec, *currentRec, *tempRec, *tempRec2;
void InsertRec() {
  cin.ignore(200,'n');
  cout<<"Enter your Friend's Name            : ";
  cin.ignore(1,'n');
  getline(cin,FriendName);
  cout<<"Enter your Friend's Mobile Number   : ";
  cin>>MobileNumber;
  cout<<"Enter your Friend's Address         : ";
  cin.ignore(1,'n');
  getline(cin,FriendAdd);
  cout<<"Enter your Friend's Email Add       : ";
  getline(cin,EmailAdd);
  cout<<endl<<endl;
  tempRec = new(personData);
  tempRec->namePerson = FriendName;
  tempRec->addressPerson = FriendAdd;
  tempRec->emailAddress = EmailAdd;
  tempRec->age = MobileNumber;
  tempRec->next=firstRec;
  //firstRec->next=tempRec;
  firstRec=tempRec;
}

整型类型的大小取决于目标平台和编译器设置。假设 long 是 32 位整数,则它仅支持 10 位数字。

32 位整数类型的最大值为:

签名:-2147483647 至 2147483647
无符号:0 到 4294967295

更好的方法是将手机号码存储为字符串,而不是数字。

这是您选择使用手机号码的数据类型问题。输入手机号码后,再次打印,看看它是否显示相同的值。您的 11 位数字值超出了long限制。

电话号码不是整数,而是一串数字。

有符号整数(对于 32 位整数,使用的取决于平台和编译器)可以包含的最大数字为 +2,147,483,647。这是 10 位数字。

相关文章: