A small problem with my first program using structures

First off, this is not a homeschool assignment, I wanted to make this after completing Einsteins Puzzle /link available on request/.

This code is supposed to create a random combination of house position, color, nationality and so on. But for some reason it cannot run, and I get an error in my random_shuffle(highlighted) that I cannot find the answer for. Codeblocks takes me to a separate tap with advanced debugging language I cannot understand.
I would appreciate if anyone would take a quick look at the setup of my struct, arrays and random_shuffle.

Below is advanced debugging code.
1
2
3
if (__first != __last)
	for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
	  std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));


Also my first post in here :)


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iterator>
using namespace std;

int positions[5]={1,2,3,4,5};
string colors[5]={"blue","yellow","red","green","white"};
string nationalities[5]={"Dane","Swede","German","Englishman","Norwegian"};
string drinks[5]={"water","bier","coffee","tea","milk"};
string smokes[5]={"Pall Mall","Prince","Blue Maters","Dunhill","Blend"};
string pets[5]={"Fish","Cats","Horses","Dogs","Birds"};

struct PlayerInfo
{
int position;
string color;
string nationality;
string favorite_drink;
string favorite_smoke;
string pet;
};

int main ()
{
    srand(time(NULL)); /*Randomize the program*/
    /*Shuffle the arrays*/
    random_shuffle(positions[0],positions[5]);
    random_shuffle(colors[0],colors[5]);
    random_shuffle(nationalities[0],nationalities[5]);
    random_shuffle(drinks[0],drinks[5]);
    random_shuffle(smokes[0],smokes[5]);
    random_shuffle(pets[0],pets[5]);
    /*Assign the arrays to each house*/
    PlayerInfo Player[5];
    for (int i=0;i<5;i++)
    {
        Player[i].position=positions[i];
        Player[i].color=colors[i];
        Player[i].nationality=nationalities[i];
        Player[i].favorite_drink=drinks[i];
        Player[i].favorite_smoke=smokes[i];
    }
    /*Tell the user the combinations created*/
    for (int i=0;i<5;i++)
    {
        cout<<"The house nr. "<<Player[i].position<<" is "<<Player[i].color<<" and is inhabited by a "<<Player[i].nationality<<
        " who drinks "<<Player[i].favorite_drink<<" and smokes "<<Player[i].favorite_smoke<<" and enjoys the company of "<<Player[i].pet<<"."<<endl<<endl;

    }

}
Standard algorithm random_shuffle requires iterators as arguments. So instead of

random_shuffle(positions[0],positions[5]);

use

random_shuffle(positions, positions +5 );
Last edited on
Thank you a bunch, didn't know that!
Topic archived. No new replies allowed.