> For the complete documentation index, see [llms.txt](https://learn.qanplatform.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.qanplatform.com/developers/qvm-multi-language-smart-contracts/docs-for-supported-languages/c++-smart-contract/writing-a-smart-contract-in-c++.md).

# Writing a smart contract in C++

### Sample contract functionality

The sample contract serves as a demonstration of general QVM logic.

It is capable of:

* registering users into the database
* storing the last registered user's name in the database
* echoing the previous user's name to STDOUT
* incrementing the total registered user count
* storing above counter in the database

### Sample code

```cpp
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

// Copied from C version
static int envAtoi(const char* env) {
    const char* val = getenv(env);
    if (val == NULL) {
        return 0;
    }
    return atoi(val);
}

int main(int argc, char *argv[]) {

    // THIS SAMPLE ONLY SUPPORTS THE "register" FUNCTION
    if (argc == 3 && strcmp(argv[1], "register") == 0) {

        // GET THE CURRENT USER'S NAME
        const char *previousName = getenv("DB_USER_CURRENT");

        // OR DEFAULT TO "unknown" IF THIS IS THE FIRST CALL
        if (previousName == NULL || strlen(previousName) == 0) {
            previousName = "unknown";
        }

        // GET THE TOTAL USER COUNT
        int totalUserCount = envAtoi("DB_TOTALUSERS");

        // WRITE PREVIOUS USER NAME TO STDOUT
        cout << "OUT=prevname: " << previousName << endl;

        // UPDATE CURRENT USER NAME BY WRITING IT TO DB
        cout << "DBW=USER_CURRENT=" << argv[2] << endl;

        // STORE USER NAME UNDER A STORAGE SLOT FOR PERSISTENCE (CURRENT GETS OVERWRITTEN ON EACH CALL)
        cout << "DBW=USER_" << totalUserCount << "=" << argv[2] << endl;

        // INCREMENT THE TOTAL USER COUNT
        cout << "DBW=TOTALUSERS=" << totalUserCount+1 << endl;
        return 0;
    }
    if (argc >= 2) {
        cerr << "Wrong CMD: " << argv[1] << endl;
        exit(1);
    }
    cerr << "Wrong args!" << endl;
    exit(1);
}
```

### Save the contract

Open a text editor and save above sample contract as `main.cpp` file.

{% content-ref url="/pages/MME4ivUqmbjy73RmSvXj" %}
[Compiling a smart contract in C++](/developers/qvm-multi-language-smart-contracts/docs-for-supported-languages/c++-smart-contract/compiling-a-smart-contract-in-c++.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.qanplatform.com/developers/qvm-multi-language-smart-contracts/docs-for-supported-languages/c++-smart-contract/writing-a-smart-contract-in-c++.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
