-8

Whenever I fetch data from an array it mixes it with another array and then outputs it, it only seems to mix the first character in the array with another array.

in this image it shows my arrays, whenever I fetch the contents of array (string HP_D) it always mixes it with sledge from the array (string S_A). this is the output, as you can see "Sledge" is not meant to be there

Here is the code:

#include <conio.h>
#include <iostream>
#include <string.h>
#include <random>
using namespace std;




FUNCTION()
{
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(1,34);
    auto random_integer = uni(rng);
    return random_integer;
    
}
FUNCTION2()
{
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(1,14);
    auto random_integer2 = uni(rng);
    return random_integer2;
}
FUNCTION3()
{
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(1,27);
    auto random_integer3 = uni(rng);
    return random_integer3;
}
FUNCTION4()
{
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(1,5);
    auto random_integer4 = uni(rng);
    return random_integer4;
}
FUNCTION5()
{
    random_device rd;
    mt19937 rng(rd());
    uniform_int_distribution<int> uni(1,35);
    auto random_integer5 = uni(rng);
    return random_integer5;
}



int main()
{
    int Operator_type;
    int Player_Count;
    int again = 10;
     
    
    cout << "How Many Players?\n";
    cin >> Player_Count;
    cin.get();
    do
    {
    
    
        cout << "('1' = ATTACKERS)\n('2' = DEFENDERS)\n('3' = SHOTGUN ONLY ATTACKER)\n('4' = SHOTGUN ONLY DEFENDER)\n('5' = HEAVY PISTOL ATTACKER)\n('6' = HEAVY PISTOL DEFENDER)\n";
        cin >> Operator_type;
    
    
    
        string DEFENDERS[34] = {"Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan",
        "Tchanka","Jaegar","Bandit","Frost","Valkyrie","Caviera","Echo","Mira","Lesion",
        "Ela","Vigil","Maestro","Alibi","Clash","Kaid","Mozzie","Warden","Goyo","Wamai",
        "Oryx","Melusi","Aruni","Thunderbird","Thorn","Azami","Solis","Fenrir" };

        string ATTACKERS[34] = {"Sledge","Thatcher","Ash","Thermite","Twitch","Montagne",
        "Glaz","Fuze","Blitz","IQ","Buck","Capitão","Hibana","Jackal","Ying","Dokkaebi",
        "Lion","Finka","Maverick","Nomad","Gridlock","Nokk","Amaru","Kali","Iana","Ace",
        "Zero","Flores","Osa","Sens","Grim","Brava","Ram"};
    
        string S_A[14] = {"Sledge","Thatcher","Thermite","Twitch","Hibana","Jackal","Ying",
        "Dokkaebi","Lion","Finka","Gridlock","Nokk","Ace","Amaru"};

        string S_D[27] = {"Smoke","Mute","Castle","Pulse","Doc","Rook","Jaeger","Bandit","Frost",
        "Valkyrie","Caviera","Echo","Mira","Lesion","Ela","Vigil","Alibi","Maestro","Clash",
        "Kaid","Warden","Goyo","Oryx","Melusi","Thunderbird","Thorn","Azami"}; 

        string HP_A[5] = {"Twitch","Montagne","Blackbeard","Nomad","Nokk"};
    
        string HP_D[5] = {"Doc","Rook","Valkyrie","Kaid","Azami"};
    
        if (Operator_type == 2){
            for (int i = 1; i <= Player_Count; ++i ) {
                cout << "Player: " << i << " = " << DEFENDERS[FUNCTION()] << "\n";
            }
            
        }   
    
        if (Operator_type == 1){
            for (int i = 1; i <= Player_Count; ++i ) {
                cout << "Player: " << i << " = " << ATTACKERS[FUNCTION5()] << "\n";
            }
        }

        if (Operator_type == 3){
            for (int i = 1; i <= Player_Count; ++i ) {
                cout << "Player: " << i << " = " << S_A[FUNCTION2()] << "\n";
            }
        }
        
        if (Operator_type == 4){
            for (int i = 1; i <= Player_Count; ++i ) {
                cout << "Player: " << i << " = " << S_D[FUNCTION3()] << "\n";
            }
        }
        
        if (Operator_type == 5){
            for (int i = 1; i <= Player_Count; ++i) {
                cout << "Player: " << i << " = " << HP_A[FUNCTION4()] << "\n";
            }
        }
        
        if (Operator_type == 6){
            for (int i = 1; i <= Player_Count; ++i ) {
                cout << "Player: " << i << " = " << HP_D[FUNCTION4()] << "\n";
            }
        }
        cout << "REROLL? (TYPE 9)\n:";
        cin >> again;

        
            
    }
    while (again == 9);

this is my code for it. It doesnt mix any other arrays, just of array "S_A" and "HP_D".

I have not thought of any method as I cannot think of a solution that would work.

  • 2
    *I dont know where the problem is?* -- Did you check if `FUNCTION1()`, `FUNCTION2()`, etc. return values that are within bounds of the array(s)? – PaulMcKenzie Aug 29 '23 at 19:54
  • 1
    Those functions don't seem to have a return type. If you are trying to get the compiler to infer the return type, do it with `auto` and make sure your compiler's reasonably up-to-date. – user4581301 Aug 29 '23 at 19:54
  • Off-topic -- If you used arrays more strategically, and removed the code duplication in all of those `FUNCTION` calls, this program would be half the size it is now, if not more. – PaulMcKenzie Aug 29 '23 at 19:57
  • 2
    Arrays are zero based, so if there are 34 members the index values should be 0 to 33, not 1 to 34. – BoP Aug 29 '23 at 20:10
  • Side note: `mt19937 rng(rd());` is very expensive. You don't want to do it every time. Standard usage is to do it once and cache `rng` somewhere visible to all of the functions that need it, in an [anonymous namespace](https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces) for example. – user4581301 Aug 29 '23 at 20:21
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Aug 29 '23 at 21:59

0 Answers0