Disadvantages of scanf

Most of the answers so far seem to focus on the string buffer overflow issue. In reality, the format specifiers that can be used with scanf functions support explicit field width setting, which limit the maximum size of the input and prevent buffer overflow. This renders the popular accusations of string-buffer overflow dangers present in … 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

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

std::cin input with spaces?

It doesn’t “fail”; it just stops reading. It sees a lexical token as a “string”. Use std::getline: #include <string> #include <iostream> int main() { std::string name, title; std::cout << “Enter your name: “; std::getline(std::cin, name); std::cout << “Enter your favourite movie: “; std::getline(std::cin, title); std::cout << name << “‘s favourite movie is ” << title; … Read more

How do you implement a good profanity filter?

Obscenity Filters: Bad Idea, or Incredibly Intercoursing Bad Idea? Also, one can’t forget The Untold History of Toontown’s SpeedChat, where even using a “safe-word whitelist” resulted in a 14-year-old quickly circumventing it with: “I want to stick my long-necked Giraffe up your fluffy white bunny.” Bottom line: Ultimately, for any system that you implement, there … Read more

Password masking console application

Console.Write(“\b \b”); will delete the asterisk character from the screen, but you do not have any code within your else block that removes the previously entered character from your pass string variable. Here’s the relevant working code that should do what you require: var pass = string.Empty; ConsoleKey key; do { var keyInfo = Console.ReadKey(intercept: … 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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)