Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
質問はすべてタイトルにあります。私は電卓を作っていますが、明らかに入力が必要です。私は cin>> 関数を使用していますが、入力をテストして数値かどうかを調べる方法があるかどうか疑問に思っていました。数字以外を入力すると、プログラムがクラッシュします。組み込みの関数/演算子はありますか? 助けてください!
入力演算子は、入力が数値の場合にのみ整数を読み取ります。それ以外の場合、文字は入力バッファーに残ります。
このようなことを試してください
int i; if (cin >> i) { // input was a number } else { // input failed }
atoiとsscanfあなたの友達ですか、または入力された文字コードが「0」から「9」の範囲内にあるかどうかを比較してください
atoi
sscanf