-2

So, i wanted to create a program that picks out simple strings from an array, it sounded pretty simple to me but then i ran into problems. Here's what i wrote:

int pickjob;
string jobs[] = {Police Officer, Fireman, Vet, Doctor, Nurse, Chemist, Waiter};
job1 = jobs[rand()%7];
job2 = jobs[rand()%7];
job3 = jobs[rand()%7];
cout << "Here i found some jobs for you, check them out\n1." << job1 << "\n2." << job2 << "\n3." << job3 << "\nGo Head and pick one out" << endl;
cin >> pickjob;
//Rest of code is below, i'll put it in if you need it ;)

But my problem was that everytime i ran the program the same jobs appeared everytime (Im a amaetur at C++, so if i sound stupid forgive me), so how do i make a program where it prints out diffrent jobs every time from the array.

Michael Webb
  • 3
  • 1
  • 4
  • Look at the `` header for C++11 and higher. – cadaniluk Jan 06 '16 at 15:21
  • Did you call `srand()` before this? – NathanOliver Jan 06 '16 at 15:21
  • when i tried srand() it said some error one sec lemme past it – Michael Webb Jan 06 '16 at 15:23
  • When trying something leads to an error, the correct response is not to abandon what you've tried. Rather, the correct response is to understand the error (which is a requirement before you can decide to either abandon what you've done, or fix it). `rand()` is not really random. If you do not seed its starting point (with `srand()`), you will get exactly the repeated results you're trying to cure. – mah Jan 06 '16 at 15:25
  • it said too few arguments for the srand function – Michael Webb Jan 06 '16 at 15:26
  • @MichaelWebb that seems like an easy fix: read its documentation. You need to be in the habit of reading the documentation of functions you use (especially when you run into something like this) before asking for help, if you want to be a successful developer. – mah Jan 06 '16 at 15:28
  • yea do you know a good website to read documentation – Michael Webb Jan 06 '16 at 15:34

1 Answers1

1

Add srand (time(NULL)); at the top of your program. Also add the header <time.h>

Read more here - link

I recommend you look into the <random> header.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Tarik Neaj
  • 548
  • 2
  • 10