How to output variables using outtextxy?

When using the outtextxy function to display a variable, it is necessary to convert the variable into a string format before using the outtextxy function to display it.

Here is an example code demonstrating how to output the value of the variable num.

#include <graphics.h>
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    int num = 10;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");

    // 将变量num转换为字符串格式
    stringstream ss;
    ss << num;
    string str = ss.str();

    // 输出变量num的值
    outtextxy(100, 100, str.c_str());

    getch();
    closegraph();
    return 0;
}

In the above code, we first convert the variable num to a string format and then display it on the screen. It is important to note that the outtextxy function can only accept character arrays (C-style strings), so we need to use the c_str() function to convert a variable of type string to a character array.

bannerAds