我希望我的程序的标题栏是一个数组中的随机字符串。我使用FreeGLUT来初始化窗口( "glutCreateWindow()“函数),但是我不确定它是如何工作的。
这就是我所拥有的:
std::string TitleArray[] =
{
"Window title 1",
"Window title 2",
"Window title 3",
"Window title 4",
"Window title 5"
};
std::string wts = TitleArray[rand() % 6];
const char* WINDOW_TITLE = wts.c_str();下面是"glutCreateWindow()“调用:
glutCreateWindow(WINDOW_TITLE);但是,每当我调试时,标题栏都是空白的。"glutCreateWindow()“函数也需要一个const char*,所以我不能简单地将'wts‘变量放在参数中。
发布于 2012-03-29 05:21:01
不确定问题是什么,除了%6而不是%5。以下是一个示例控制台程序,显示了rand()的使用:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <time.h>
std::string TitleArray[] =
{
"Window title 1",
"Window title 2",
"Window title 3",
"Window title 4",
"Window title 5"
};
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
srand ( time(NULL) ); // seed with current time
for(int i=0; i<20; ++i)
{
std::string wts = TitleArray[rand() % 5];
cout << wts.c_str() << endl;
}
return 0;
}
Console output:
Window title 3
Window title 4
Window title 5
Window title 2
Window title 4
Window title 4
Window title 1
Window title 3
Window title 2
Window title 1
Window title 2
Window title 1
Window title 2
Window title 5
Window title 4
Window title 5
Window title 3
Window title 1
Window title 4
Window title 1
Press any key to continue . . .如果省略srand()或始终使用相同的种子,则每次运行都将获得相同的输出。
https://stackoverflow.com/questions/9900840
复制相似问题