Skip to main content

Thread: Beginner question: Restricting negative-numbers input into unsigned types?


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; }
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.

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

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support