佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1597|回复: 3

C++ & DirextX9 问题~ 帮帮忙下

[复制链接]
发表于 22-11-2008 11:20 PM | 显示全部楼层
#   include   <iostream>   
  #   include   <string>
int num1 = 1;
int num2 = 2;
string   temp;
temp=sprintf(str,"%d",num1)+sprintf(str,"%d",num1);
试试吧
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 22-11-2008 01:12 PM | 显示全部楼层 |阅读模式
想请教下~
例子:-
int num1 = 1;
int num2 = 2;
char operator = '+';
cout << num1 << a << num2 << " = " << num1+num2  << endl; //console output
在C++要怎样改才能把cout的整句换去string?
我要用dirext的DrawTextA来output..

老师要我们做小游戏~console的就会
当要convert 去 GUI 就头大~~   
帮帮忙下~谢了~
回复

使用道具 举报

发表于 28-11-2008 11:47 PM | 显示全部楼层
  1. int sprintf ( char * str, const char * format, ... );
  2. ....
  3. Return Value
  4. On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.
  5. On failure, a negative number is returned.

  6. 来自:http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html
复制代码
我相信楼主想要问的是如何把 “[num1] [operator] [num2] = [sum of num1 and num2]" 这个句子化成 C String (或者char *)以便提供给 DrawTextA()使用。

看了上面的sprintf()的defination,就可发现,冰室大大的答案完全错误。temp的内容将会相当于2,因为每一个sprintf()都会return 1(因为每一个sprintf()都会返回所写出的字元的数量,在这里,num1 和 num2 都只是单个数字),所以 temp = 1 + 1 => temp = 2。

正确答案是:
  1. char buffer[MAX_BUFFER]; //MAX_BUFFER can be any number,
  2. int num1 = 1, num2 = 2;
  3. char operator = '+';

  4. sprintf(buffer, "%d %c %d = %d", num1, operator, num2, num1+num2);
复制代码

[ 本帖最后由 geekman 于 28-11-2008 11:51 PM 编辑 ]
回复

使用道具 举报

发表于 6-3-2009 09:25 PM | 显示全部楼层

回复 1# ahwai89 的帖子

可以用stringstream

#include <sstream>

.......

stringstream ss;
ss << num1 << a << num2 << " = " << num1+num2  << endl

要 convert string 就
ss.str();
convert c string 就

ss.str().c_str();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 17-12-2025 06:15 AM , Processed in 0.101620 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表