首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为窗口标题栏使用随机字符串?

如何为窗口标题栏使用随机字符串?
EN

Stack Overflow用户
提问于 2012-03-28 11:41:56
回答 1查看 899关注 0票数 0

我希望我的程序的标题栏是一个数组中的随机字符串。我使用FreeGLUT来初始化窗口( "glutCreateWindow()“函数),但是我不确定它是如何工作的。

这就是我所拥有的:

代码语言:javascript
复制
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()“调用:

代码语言:javascript
复制
glutCreateWindow(WINDOW_TITLE);

但是,每当我调试时,标题栏都是空白的。"glutCreateWindow()“函数也需要一个const char*,所以我不能简单地将'wts‘变量放在参数中。

EN

回答 1

Stack Overflow用户

发布于 2012-03-29 05:21:01

不确定问题是什么,除了%6而不是%5。以下是一个示例控制台程序,显示了rand()的使用:

代码语言:javascript
复制
#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()或始终使用相同的种子,则每次运行都将获得相同的输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9900840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档