-1
我想要防止用戶在提示時輸入錯誤的數據類型的可能性。C++:獲取用戶通過cin輸入的數據類型
例如,假設:
int age
cout << "What is your age?" << endl;
cin >> age;
但可以說,在字符串中的用戶類型或一個字符代替,會有反正我抓住它,並重新提示用戶再試一次嗎?像:
if (age != dataType(int) || age !=dataType(float)
cout << "Invalid entry. Please enter a number and try again.";
編輯:據我所知,CIN可以在字符或字符串,但我需要檢查,看它是否符合整型,因爲在這個例子中,用戶可以通過它上面輸入他或她的年齡例如「二十三」。
首先讀入一個'std :: string',然後用'std :: istream'來使用這個''std :: istream'',這樣你可以檢查數字提取是否失敗。 –
我想你可能會覺得這很有幫助。 http://stackoverflow.com/questions/12721911/c-how-to-verify-is-the-data-input-is-of-the-correct-datatype#12722150 –