看板 ck55th309 關於我們 聯絡資訊
有個很tricky但很簡單的程式碼 char input[50]; char temp[50]; int integernumber; sscanf( input, "%d", &integernumber); sprintf(temp , "%d", integernumber); if( ! strcmp( input, temp ) ) printf("it's an integer number\n"); 同理,也可以檢測float, double等等... ※ 引述《pleb (I am nobody!)》之銘言: : 我是先用char : 一個一個抓 : 然後再判斷是不是我要的型態 : 例如, : char number[50]; : cin>>number; : newline();//將多餘的字串刪掉 : for(int i=0;i<strlen(number);i++) : { : if(!(isdigit(number[i]))//輸入的不是數字的話 : ans=true;//ans = true : ...................... : ...................... : } : isdigit是在一種函式庫,好像是在<cstdlib>或是<cctype> : 試一下吧~ : 不知道有沒有幫到你? : ※ 引述《GodOfTiger (*桃花運招來*)》之銘言: : : 怎樣判斷使用者輸入之資料的型態? : : 會的幫幫忙吧 感恩 -- 我的相簿: http://photo.jojo.idv.tw/browse.php?albumid=1 我寫的相簿系統: http://photo.jojo.idv.tw -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.141.98