Hi, i am making a blackjack program in Visual Basic for a college project, in order to generate a random card each time, i need to make a random number, after numerous failures to do this using VB, i have made a small program in C++ that generates a random number to a text file when it is executed. The C++ program works on its own but when i try to execute it using VB's shell() function it seems to work, as a console window flashes on screen, but the actual program does not run correctly, as the random number in the text file is always the same. Does anyone have any ideas on how to make this program work as it should when called from VB ?

Recommended Answers

All 10 Replies

Call Shell(App.Path & "\random.exe", vbHide)
Open App.Path & "\random.txt" For Input As #1
Input #1, RandomNumber
Close #1

txtRandom.Text = RandomNumber

This is a snippet of my code for the program

why don't you random number using VB, why using C++ random number ?

i tried many times to do that, but failed, I have been doing C++ a while and i already know how to make random numbers, so i decided just to use that, any suggestions on how i could make a random number, hopefully unique from 1 - 52 or and ideas on why shell() wont excecute my program properly ?

maybe something wrong with your C++ code, can you post your C++ random number code, and also check your "\random.txt", if there something wrong with the file.

--------------------------------------------------------
and here how to random number in VB

Randomize
    RandomNumber = Int(Rnd * 10)

this code is random from 0 to 10

ok thanks a lot, however my c++ code is fine, I know this because when i run the program manually and check the contents of the text file it changes every time.

how many line in your text file ?

i have just the 1 line in my text file, just a number between 1 and 52. My VB code to extract the value works fine, as it actually takes the value from the text file and uses it to represent a card, the problem is that when i use shell() to execute the C++ program, it doesnt seem to execute correctly, as the value from the text file stays the same unless it is executed manually

Is it possible that the vb open command is not waiting for the shell command to finish? You could make sure that VB deletes the random file, and then waits for the random file to exist again before finishing a doevents loop.... for example:

' /* Check If The Random File Exists */
if dir(app.path & "\random.txt", vbnormal) <> "" then 
     ' /* If So, Remove The Random File */
     kill app.path & "\random.txt"
end if

' /* Ok, Launch random.exe */
Shell App.Path & "\random.exe", vbHide

' /* Loop Until The Random Text File Exists Again */
do until dir(app.path & "\random.txt") <> ""
     ' /* Process System Events */
     doevents
loop

' /* Open The Random Text File For Reading */
Open App.Path & "\random.txt" For Input As #1
     ' /* Read In The Random Number */
     Input #1, RandomNumber
Close #1

' /* Set The TextBox To The Random Number */
txtRandom.Text = RandomNumber

I'm not sure how that will work, but let me know how it turns out.

I test it, it is working fine, Here this is what i am testing with

random.exe code in C++

#include <time.h>
#include <fstream>
using namespace std;

void main()
{
	ofstream myfile("c:/random.txt");
	srand(time(NULL));
	myfile << rand() % 50;
	myfile.close();
	return;
}

and VB code

Private Sub Form_Load()

Call Shell("c:\random.exe")
Open "c:\random.txt" For Input As #1
    Input #1, RandomNumber
Close #1
MsgBox (RandomNumber)

End Sub

and the RandomNumber is alway change.

lol sorry for my late reply, but thank you very much for your responses. Do you have any idea now how to make it so that a unique card is selected each time, the methods i have tried with setting variables to an ID that points to a card and then checking the variables each time does not seem to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.