outtextxy输出变量

0
#include<iostream>
#include<graphics.h>
#include<string>
#include <sstream>
using namespace std;
int main(){
int sjs=2;
stringstream ss;
string jie;
ss << sjs+1;
ss >> jie;

outtextxy(1230,5,jie);
}

我用的是vs2019,请问c++如何用outtextxy输出string变量

ava
system("cls");

2023-7-27

1

参考 outtextxy 函数官方用法与教程:https://docs.easyx.cn/zh-cn/outtextxy

你的代码需要先 initgraph 初始化绘图窗口再调用 outtextxy 函数,否则会报错的。

要输出 string,先要明确你的代码用的什么字符集。如果用的 MBCS 字符集(VC6 默认),可以用 string。如果用的 Unicode 字符集,需要用 wstring。
string 或 wstring 的成员 c_str() 可以获得字符串指针。你的代码这样修改:

#include <graphics.h>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

int main()
{
	// 创建绘图窗口
	initgraph(640, 480);

	int sjs=2;
	wstringstream ss;	// 如果是 MBCS 字符集,将 wstringstream 修改为 stringstream
	wstring jie;		// 如果是 MBCS 字符集,将 wstring 修改为 string
	ss << sjs+1;
	ss >> jie;

	outtextxy(50, 5, jie.c_str());

	// 按任意键退出
	getmessage(EX_CHAR);
	return 0;
}

如果你只想用 outtextxy 输出整型变量 sjs,也可以将 sjs 整形变量转换成字符串变量再输出,这样做:

#include <iostream>
#include <graphics.h>
#include <string>
#include <sstream>
using namespace std;
int main()
{
	// 创建绘图窗口
	initgraph(500, 500);

	int sjs=2;

	// 声明字符串变量
	TCHAR a[100];
	// 把整形变量 sjs 转换成字符串变量,才能用 outtextxy 输出
	_stprintf_s(a, _T("%d"), sjs + 1);

	// 输出 sjs 计算结果
	outtextxy(50, 5, a);

	// 按任意键退出
	getmessage(EX_CHAR);
	closegraph ();
	return 0;
}
ava
随波逐流

2023-7-27

技术讨论社区
相关提问