I have recently started coding in c++ and have made a simple console application. When executed the program asks the user to position their cursor wherever they want continuous repeating clicks, after 7 seconds it then asks the user to set an interval for the clicks. Since I am fairly new I am wondering if there is anything I could have done better to optimise the performance of the code or any other tips and tricks I can use, much appreciated

my code is (main.cpp):

#include <iostream>
#include <Windows.h>

using namespace std;

void leftClick(double mouse_x, double mouse_y){
        INPUT input;
        input.mi.dx=(mouse_x * 65536) / GetSystemMetrics(SM_CXSCREEN) + 1;
        input.mi.dy=(mouse_y * 65536) / GetSystemMetrics(SM_CYSCREEN) + 1;

int main(){
    cout << "Place the cursor where you want to click..." << endl;

    POINT p;

    cout << "Set the interval to click: ";

    int cinterval {0};
    cin >> cinterval;

    cout << "Current Interval: " << cinterval * 1000 << endl;

        leftClick(p.x, p.y);
        Sleep(cinterval * 1000);

     return 0;
  • \$\begingroup\$ (Tricks - when coding, know them, but keep your code simple.) \$\endgroup\$ – greybeard Feb 13 '20 at 2:22

Here are some suggestions you can use to improve your code.


Insert a space before a { that starts a compound statement and does not go on a separate line; e.g., instead of int main(){, write

int main() {


int main()

Place a space after a comma and after a control word (while, etc.): SendInput(1, &input, sizeof(INPUT)) and while (true) {.

Place two spaces around the = operator.

The indentation level of the leftClick function should be reduced to 4 spaces to keep consistent with main.


In C++, functions are usually named in snake_case instead of camelCase. This is subjective though.

Standard library usage

Please try to avoid using namespace std; because it is considered bad practice. The contents of the C++ standard library are put in a special namespace to avoid polluting the global namespace and causing name clashes. The std namespace is not intended for use with using directives (in general). using namespace std; forces you to avoid standard names and invalidates the purpose of the std namespace. See Why is “using namespace std;” considered bad practice?.

std::endl is generally inferior to \n, but it is necessary in this case because of flushing requirements. Good.

Currently, your code isn't clear in terms of unit of time. Is the interval intended to be in seconds, milliseconds, or microseconds? The standard <chrono> library solves this problem. And instead of Sleep, use std::this_thread::sleep_for from <thread>, which plays well with <chrono>:

using namespace std::chrono_literals;

std::cout << "Place the cursor where you want to click ..." << std::endl;
std::this_thread::sleep_for(7s); // NB: valid syntax!

Indicate your presumed unit of time:

cout << "Set the interval to click (ms): ";

int cinterval {0};
cin >> cinterval;

// ...
  • \$\begingroup\$ I would also suggest setting a minimum interval time to 16 milliseconds, i.e. once per frame at 60 frames per second. That way you don't flood the input queue and cause the target application to lock up. \$\endgroup\$ – Casey Nov 9 '20 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.