[英]C++ program terminate when entering more than 11 integer
我有一個用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
更好的方法是將手機號碼存儲為字符串,而不是數字。
電話號碼不是整數,而是一串數字。
一個有符號整數(用於32位整數,取決於您的平台和編譯器)可以包含的最大數量為+2,147,483,647。 這是10位數字。
這是您選擇使用手機號碼的數據類型的問題。 輸入手機號碼后,再次打印它,看看它是否顯示相同的值。 您的11位數字超出了long
限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.