Click here to Skip to main content
15,917,645 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Help with GetPrivateProfileSectionNames Pin
PJ Arends10-Jun-06 9:11
professionalPJ Arends10-Jun-06 9:11 
AnswerRe: Help with GetPrivateProfileSectionNames [modified] Pin
Mark F.10-Jun-06 10:20
Mark F.10-Jun-06 10:20 
QuestionRe: Help with GetPrivateProfileSectionNames Pin
fury 8410-Jun-06 12:02
fury 8410-Jun-06 12:02 
AnswerRe: Help with GetPrivateProfileSectionNames Pin
PJ Arends10-Jun-06 12:07
professionalPJ Arends10-Jun-06 12:07 
QuestionArray...with out redunancy (Urgent is pls) Pin
RockyJames10-Jun-06 6:49
RockyJames10-Jun-06 6:49 
AnswerRe: Array...with out redunancy (Urgent is pls) Pin
Chris Losinger10-Jun-06 7:42
professionalChris Losinger10-Jun-06 7:42 
AnswerRe: Array...with out redunancy (Urgent is pls) Pin
Kevin McFarlane10-Jun-06 7:50
Kevin McFarlane10-Jun-06 7:50 
QuestionRandom combinations (standard c++) [modified] Pin
Peter Charlesworth10-Jun-06 6:05
Peter Charlesworth10-Jun-06 6:05 
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
AnswerRe: Random combinations (standard c++) Pin
Justin Tay10-Jun-06 6:18
Justin Tay10-Jun-06 6:18 
GeneralRe: Random combinations (standard c++) Pin
Peter Charlesworth10-Jun-06 12:22
Peter Charlesworth10-Jun-06 12:22 
QuestionLink Error Pin
Sapto Priyono10-Jun-06 4:40
Sapto Priyono10-Jun-06 4:40 
AnswerRe: Link Error Pin
Chris Losinger10-Jun-06 8:15
professionalChris Losinger10-Jun-06 8:15 
Questionhow can i control my microphone volume [modified] Pin
happy_ram10-Jun-06 1:11
happy_ram10-Jun-06 1:11 
QuestionHow to slove the problems with Movewindow? Pin
G Haranadh9-Jun-06 23:23
G Haranadh9-Jun-06 23:23 
AnswerRe: what are the problems we can face with Movewindow Pin
Amar Sutar10-Jun-06 0:04
Amar Sutar10-Jun-06 0:04 
GeneralRe: what are the problems we can face with Movewindow Pin
G Haranadh10-Jun-06 7:48
G Haranadh10-Jun-06 7:48 
Questionhelp me out for spawning error. Pin
nitinjdalal9-Jun-06 21:26
nitinjdalal9-Jun-06 21:26 
AnswerRe: help me out for spawning error. Pin
Aqueel10-Jun-06 1:50
Aqueel10-Jun-06 1:50 
Generalhelp me out for spawning error Pin
nitinjdalal9-Jun-06 21:22
nitinjdalal9-Jun-06 21:22 
GeneralRe: help me out for spawning error Pin
Cedric Moonen9-Jun-06 22:18
Cedric Moonen9-Jun-06 22:18 
Questionhow to store image file in ORACLE database not in MSACCESS using VC++ [modified] Pin
maharaja pandian9-Jun-06 20:57
maharaja pandian9-Jun-06 20:57 
QuestionRe: how to store image file in ORACLE database not in MSACCESS using VC++ Pin
David Crow14-Jun-06 8:09
David Crow14-Jun-06 8:09 
Questionp/w dialog box before CView Pin
Tara149-Jun-06 20:35
Tara149-Jun-06 20:35 
AnswerRe: p/w dialog box before CView [modified] Pin
Amar Sutar9-Jun-06 21:25
Amar Sutar9-Jun-06 21:25 
GeneralRe: p/w dialog box before CView Pin
Tara149-Jun-06 21:35
Tara149-Jun-06 21:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.