2012-03-28 9 views
0

私のプログラムのタイトルバーを配列のランダムな文字列にします。私は、ウィンドウを初期化するために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'変数を置くことはできません。

+1

インデックスが0〜4であるため、配列アクセスを 'rand()%5'に変更したいことがあります。しかしそれがあなたの状況を解決するかどうかは分かりません。 –

+0

これは、アレイに2番目のものを表示しました。ありがとうございます。 :)どのように私は毎回別のものを表示することができるかについての任意のアイデア? – Charles

+1

@Charles:乱数ジェネレータ 'std :: srand(std :: time(nullptr));'? –

答えて

1

%5の代わりに%6以外の問題がわかりません。ここではランドの使用を示す例コンソールプログラム()である:同じシードを使用し、常にあなたがsrand関数を除外した場合

#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 . . . 

は、()または、あなたはそれぞれの実行のために同じ出力が得られます。

関連する問題