|
|
|
Hi guys!
I have picked up c++ again after a period of rest and I have written a program that is based around random numbers.
Here is what it does: it "calculates" two random numbers (both with a maximum, as you will see in the code. The maximum corresponds with the two arrays of strings stated in the beginning of the main integer) and will output the strings corresponding to those two numbers.
It will, in short, pick a person and pick an activity they can do and put these together to form something like "Mike is eating a sandwich".
In the beginning of the program, the user is asked to input an integer defining the amount of combinations he would like to receive. He might, for instance, type "4" and receive this as output:
Mike is eating a sandwich.
Jack is playing the guitar.
Bella is watching a video.
Dave is playing tennis.
But, and here comes the problem, the program will, in that case, not output those four combinations, but something like this:
Mike is eating a sandwich.
Mike is eating a sandwich.
Mike is eating a sandwich.
Mike is eating a sandwich.
In other words, the program will always use the same random number as it "calculated" for the first combination, thus coming up with the same result for every combination the user wanted.
How can I fix this?
Here's the program, written in c++ and compiled using Borland 5.5:
<br />
WhoDoesWhat.cpp<br />
#include < cstdlib ><br />
#include < ctime ><br />
#include < iostream ><br />
#include < string ><br />
using namespace std;<br />
<br />
int main()<br />
{<br />
<br />
string person[9] = {<br />
"Jason",<br />
"My best friend",<br />
"Uncle Ben",<br />
"Mark",<br />
"Eva",<br />
"Dad",<br />
"My daughter",<br />
"Deborah",<br />
"That kid over there"};<br />
<br />
string activity[9] = {<br />
" is preparing a diner for two.",<br />
" is playing a game of tennis with mum.",<br />
" is making out with his/her girl/boyfriend.",<br />
" is studying his geography exam.",<br />
" is still sleeping.",<br />
" is cruising on his motorcycle.",<br />
" is shooting baskets in the garden.",<br />
" is relaxing by the pool.",<br />
" is coding a simple program in C++"};<br />
<br />
int a = 0;<br />
int b;<br />
int i;<br />
int j;<br />
const int N = 9;<br />
<br />
cout << "Who is doing what? Find out with this program!\n";<br />
<br />
cout << "Of how many persons would you like to know what they are doing?\nThey might be busy doing more things at a time, though!\n"; <br />
<br />
cin >> b;<br />
<br />
cout << endl;<br />
<br />
while (a < b){<br />
srand( (unsigned)time( NULL ) );<br />
<br />
for (i = 0; i < 25; i++) { <br />
rand();}<br />
for (j = 0; j < 25; j++) {<br />
rand();}<br />
<br />
i = (int) N * rand() / (RAND_MAX + a + 3.0);<br />
j = (int) N * rand() / (RAND_MAX + a + 3.0);<br />
<br />
cout << person[i] << activity[j] << endl;<br />
<br />
a++;<br />
}<br />
<br />
cout << "\n\nNow you know a bit more about the people around you!\nCome back later, you hear!";<br />
<br />
getchar();<br />
<br />
return 0;<br />
<br />
}<br />
I hope anyone can help.
Thanks in advance!
Regards,
Peter
-- modified at 12:08 Saturday 10th June, 2006
|
|
|
|
|
You only need to seed the random number generator once.
srand( (unsigned)time( NULL ) );
while (a < b){
i = rand() % N;
j = rand() % N;
cout << person[i] << activity[j] << endl;
a++;
}
|
|
|
|
|
Ah, thanks hfry!
I'll never forget not to put the number generator seeder in a loop.
|
|
|
|
|
I have the probelm for debug my VC++ project with VC 7.1. The error message :
fatal error LNK1000: Internal error during Pass2
Anyone help me?
Thx.
|
|
|
|
|
|
how can i control my microphone volume.i able record my audio using MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE.
sree.
-- modified at 7:12 Saturday 10th June, 2006
|
|
|
|
|
What are the problems we can face with Movewindow?
I used the getclient rect, got the rectangle and then
I am using the move window function.
Then here i got the problems.
The window is not moveing to the particular position.
How to solve that problems.
Any one please let me know.
Nice talking to you.
If you judge people, you have no time to love them. -- Mother Teresa
|
|
|
|
|
Try using GetWindowRect (), ScreenToClient() and then MoveWindow function.
Regards
Amar
|
|
|
|
|
Oh. Thanks a lot. It is working now.
is for you.
Nice talking to you.
If you judge people, you have no time to love them. -- Mother Teresa
|
|
|
|
|
hi,
i am working with microsoft visual c++.But i write a code it will give me
an error and the error is spawning vcspawn.exe and my program will not build
and the tool not build.so help me out for that
Thanks......
nitin
nitin
|
|
|
|
|
Go to project settings and check the paths of header files, and other libraries and files there. Make sure that they are correct.
I hope it helps
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
hi,
i am working with microsoft visual c++.But i write a code it will give me
an error and the error is spawning vcspawn.exe and my program will not build
and the tool not build.so help me out for that
Thanks......
nitin
nitin
|
|
|
|
|
|
i want to store a image file into "BLOB" value in ORACLE databse.
& i used COleVariant to store the byte array of image file.
but when i try to insert into the oracle db , it through a error "Data type Conversion error"
it's working fine for MSACCESS db using the same COleVariant class..
can u please help me for the same..
it's very urgent ....
send reply
-- modified at 3:03 Saturday 10th June, 2006
|
|
|
|
|
Any help here?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
I have created an new application in vc++6, which when run, opens with the ususl CView screen. I want to make a username and password dialog box to apperar befor the CView screen..i.e. if the u/n and p/w is correct, only then the application should open.
Please can you tell me how to do this.
Thanks.
Tara
Fortitudine Vinsinues!
|
|
|
|
|
BOOL SampleApp::InitInstance()
{
CWinApp::InitInstance();
// Display a login dialog box
CLoginDlg dlglogin;
// Check is user press cancel
// Assuming all the validation are handle before returning
if(dlgLogin.DoModal() == IDCANCEL)
return FALSE;
..
.
.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
Regards
Amar
-- modified at 3:25 Saturday 10th June, 2006
|
|
|
|
|
Thanks. I tried it, but I am geting the error ''dlgLogin' : undeclared identifier'
I have created the dialog, included the header file etc. Why is this happening?
Thanks.
Fortitudine Vinsinues!
|
|
|
|
|
CLoginDlg is not a built in class. You have to write it your own. I am giving just e.g.
Regards
Amar
|
|
|
|
|
Please can you give me an idea how to write it.
Fortitudine Vinsinues!
|
|
|
|
|
I wanan know that how can we recover data if the clusters are scattered ? I know that data can be recovered from FAT (flopy) if clusters are in a contigious space ,I dont know how to do it if we hav clusters separated on the whole FAT partition.. Plz some body help.....
|
|
|
|
|
For example, I want to create mem dc for a client window.The client size is (100, 200) ,but i want create a larger mem dc which may be (1000, 2000),
The code lists below work well on xp but fail in 2000, why?
CDC * pDC = CreateCompatibleDC(this);
CDC m_memDC;<br />
m_memDC.CreateDC( pDC );<br />
HBITMAP hbitmap = m_memDC.CreateCompatibleBitmap( width, heigth ); <br />
May be I just can create a bitmap which size is smaller than the client window on windows 2000??
if i still want to create a larger one, how to do this?
What do you think ? Can give me some help?
thanks!!
-- modified at 22:58 Friday 9th June, 2006
|
|
|
|
|
Error 87 is "The parameter is incorrect" (Use the error lookup tool that comes with VS)
Also, I didn't realize the CDC had a CreateCompatibleBitmap() member function.
And CDC::CreateDC usually takes 4 parameters, none of them a pointer to a CDC object.
I am suprised you even get the code to compile.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|