2012-12-18 58 views
-1

我有一個用C++編寫的程序,要求用戶輸入他/她的11位數字 手機號碼。但是當我輸入11位數字時,程序continue沒有 執行後面的代碼。當輸入超過11個整數時C++程序終止

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; 
} 
+6

感謝您告訴我們。有問題嗎? –

+2

我猜是因爲你忘記了你廣泛使用的cin.ignore。我會建議使用cin.getline()代替用戶輸入 - 並請:將數字不存入長變量,而是存儲爲字符串 – Najzero

+2

電話號碼應該是字符串。 – Maroun

回答

4

整數類型的大小取決於目標平臺和編譯器設置。假設long是一個32位整數,它只支持10位數字。

爲32位整數類型的最大值是:

簽署:-2147483647到2147483647
無符號:0至4294967295

一種更好的方法是將移動號碼存儲爲字符串,不是一個數字。

+3

這隻適用於* 32位*'長'。 –

+0

@PaulR long在x86上通常是32位 – JustMaximumPower

+2

@JustMaximumPower:它通常在64位上是64位的。但這與問題無關 - 電話號碼是(通常)數字的字符串,而不是整數。 –

1

電話號碼不是整數,它是一串數字。

可以包含的最大有符號整數(對於32位整數,取決於您的平臺和編譯器)是+2,147,483,647。這是10位數字。

+0

這隻適用於* 32位*整數。 –

1

這是您選擇採取手機號碼的數據類型的問題。輸入手機號碼後,再次打印,看看是否顯示相同的值。 您的11位數值超過long限制。

相關問題