Thread: Beginner question: Restricting negative-numbers input into unsigned types?
so, here have defense against bad input, how set limit on entering negative numbers? long user enters number below 0, goes past reset point unsigned long type, subtracts number 4294967296 (depending on implementation), , assigns n.code:#include <iostream> int main() { unsigned long n; cout << "enter positive number: "; while (!(cin >> n)) { cin.clear(); while (cin.get() != '\n') continue; cout << "try again: "; } return 0; }
look cin.fail(). checks whether input user fits variable.
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Beginner question: Restricting negative-numbers input into unsigned types?
Ubuntu
Comments
Post a Comment