Jul 21, 2010 at 9:02am UTC
Replace return main(); with return 0;.
Jul 21, 2010 at 9:18am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <ctime>
#include <cstdlib>
#include <windows.h>
int main()
{
srand(time(0));
int i = rand() % 2 + 1;
char buffer[5];
itoa(i,buffer,10);
MessageBox (0, buffer, "Result" , MB_OK);
return 0;
}
http://cplusplus.com/reference/clibrary/cstdlib/itoa/
Last edited on Jul 21, 2010 at 9:19am UTC
Jul 21, 2010 at 10:26am UTC
pabloist, return 0 makes the program close once it hits that, I wanted it to return to main and open another message box when I closed the current one.
m4ster r0shi, thank you this helps
Jul 21, 2010 at 8:06pm UTC
TheEliteOne wrote:I wanted it to return to main and open another message box when I closed the current one.
It's better to do it using a loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iostream>
int main()
{
srand(time(0));
std::cout << "hold esc to quit..." << std::endl;
while (true )
{
int i = rand() % 2 + 1;
char buffer[5];
itoa(i,buffer,10);
MessageBox (0, buffer, "Result" , MB_OK);
if (GetAsyncKeyState(VK_ESCAPE)>>15) break ;
}
return 0;
}
EDIT: Oh, and to avoid calling itoa all the time, you can do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iostream>
const char * num_to_text[3]={"" ,"1" ,"2" };
int main()
{
srand(time(0));
std::cout << "hold esc to quit..." << std::endl;
int i;
while (true )
{
i = rand() % 2 + 1;
MessageBox (0, num_to_text[i], "Result" , MB_OK);
if (GetAsyncKeyState(VK_ESCAPE)>>15) break ;
}
return 0;
}
Last edited on Jul 21, 2010 at 8:19pm UTC