2012-03-28 2 views
0

내 프로그램의 제목 표시 줄을 배열의 임의 문자열로 지정합니다. 나는 창 ("glutCreateWindow()"함수를 초기화하기 위해 FreeGLUT을 사용하고 있지만 어떻게 작동하는지 확신 할 수 없습니다.창 제목 표시 줄에 임의의 문자열을 사용하는 방법은 무엇입니까?

가 여기에 내가 가진 무엇 :

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

그 두 번째 배열에 표시, 감사합니다. :) 매번 다른 방법으로 표시 할 수있는 방법에 대한 아이디어가 있습니까? – 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 . . . 

가) 또는 항상 같은 종자를 사용, 각 실행에 대한 동일한 출력을 얻을 것이다.

관련 문제