How to scanf only integer and repeat reading if the user enters non-numeric characters?

Use scanf(“%d”,&rows) instead of scanf(“%s”,input) This allow you to get direcly the integer value from stdin without need to convert to int. If the user enter a string containing a non numeric characters then you have to clean your stdin before the next scanf(“%d”,&rows). your code could look like this: #include <stdio.h> #include <stdlib.h> int … Read more

JavaScript: Check if mouse button down?

Regarding Pax’ solution: it doesn’t work if user clicks more than one button intentionally or accidentally. Don’t ask me how I know :-(. The correct code should be like that: var mouseDown = 0; document.body.onmousedown = function() { ++mouseDown; } document.body.onmouseup = function() { –mouseDown; } With the test like this: if(mouseDown){ // crikey! isn’t … Read more

How to show “Done” button on iOS number pad keyboard?

Another solution. Perfect if there are other non-number pad text fields on the screen. – (void)viewDidLoad { [super viewDidLoad]; UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@”Cancel” style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@”Apply” style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]]; [numberToolbar sizeToFit]; numberTextField.inputAccessoryView = numberToolbar; } -(void)cancelNumberPad{ [numberTextField resignFirstResponder]; … Read more

JUnit testing with simulated user input

You can replace System.in with you own stream by calling System.setIn(InputStream in). InputStream can be a byte array: InputStream sysInBackup = System.in; // backup System.in to restore it later ByteArrayInputStream in = new ByteArrayInputStream(“My string”.getBytes()); System.setIn(in); // do your thing // optionally, reset System.in to its original System.setIn(sysInBackup); Different approach can be make this method … Read more

Getting a hidden password input

Use getpass.getpass(): from getpass import getpass password = getpass() An optional prompt can be passed as parameter; the default is “Password: “. Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further … Read more

raw_input without pressing enter

Under Windows, you need the msvcrt module, specifically, it seems from the way you describe your problem, the function msvcrt.getch: Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. (etc … Read more