|
查看: 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);
试试吧 |
|
|
|
|
|
|
|
|
|
|
想请教下~
例子:-
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
|
显示全部楼层
- int sprintf ( char * str, const char * format, ... );
- ....
- Return Value
- 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.
- On failure, a negative number is returned.
- 来自: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。
正确答案是:- char buffer[MAX_BUFFER]; //MAX_BUFFER can be any number,
- int num1 = 1, num2 = 2;
- char operator = '+';
- 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(); |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|