【问题标题】:Placing Random String values into an int Array将随机字符串值放入 int 数组
【发布时间】:2026-02-13 11:10:01
【问题描述】:

我对 c++ 很陌生,想知道如何将值随机放入 int 数组中。我想做的是使用此函数将值随机放入网格中,以创建内存匹配游戏。

#include "stdafx.h"
#define NOMINMAX
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void shuffleL(int [][8]);

int main()
{
                //shuffles characters
            shuffleS(charactersS);
                for (int i = 0; i <= 8; i++)
                {
                        cout << "---";
                }
                cout << endl;

                //output grid
                for (int r = 0; r < 4; r++)
                {
                    cout << r + 1 << " | ";
                    for (int c = 0; c < 4; c++)
                    {
                        cout << " [VGC] ";
                        status[r][c] = false;
                    }
                    cout << endl;
                }
                cout << endl;
}

void shuffleL(int characters[][8])
{
      string vgc[50] = { "PacMan", "Laura", "Mario", "Sonic", "Link", "Snake", "Drake",  "Samus", "MegaMan", "Kratos",
    "Isaac", "DK", "Dante", "Crash", "Spyro", "Kirby", "Ryu", "Yoshi", "Sora", "Strider",
    "DigDug", "Lil_Mac", "Pit", "Booker", "Rayman", "Frogger", "Marcus", "Shepard", "Sly", "Ezio",
    "Guybrush", "Leon", "Raz", "Ninten", "Ralph", "Crono", "MaxPayne", "Fox", "Simon", "Cole",
    "Pheonix", "Corvo", "Parappa", "Faith", "Lucas", "Scorpion", "Gordon", "Roland", "Chell", "Olimar" };

  string temp;
  for (int s = 0; s <= 4; s++)
  {
      for (int x = 0; x<16; x++)
      {
          srand((unsigned)time(NULL));
          int i = rand() % 15 + 1;
          temp = vgc[x];
          vgc[x] = vgc[i];
          vgc[i] = temp;
      }
  }
  int i = 0;
  //Input of Values in Here
  for (int r = 0; r < 50; r++)
  {
    for (int c = 0; c < 50; c++)
    {
        characters[r][c] = vgc[i]; //THIS VGC GIVES ME THE ERROR
        cout << characters[r][c];
        i = i + 1;
    }
    cout << endl;
  }
}

它还给我一个变量名(vgc)说的错误

1 IntelliSense:不存在从“std::string”到“int”的合适转换函数

我完全不知道如何解决这个问题。

【问题讨论】:

  • 您希望如何将字符串存储在 int 类型的容器中?
  • 使用字符串数组代替。
  • [OT]:您应该只使用一次srandstd::shuffle 也可能有帮助。
  • 我不确定我是否完全理解我应该做些什么来修复它

标签: c++ arrays function for-loop


【解决方案1】:

不要做 int characters [] [8]; ,而是 string characters [] [8]; 。您在数组声明开头指定的类型是数组将存储的信息类型。 和平。

【讨论】:

  • 我这样做了,但后来我收到了这个错误 Error 3 error LNK2019 : unresolved external symbol "void __cdecl shuffleL(int (* const)[8])" (?shuffleL@@YAXQAY07H@Z)在函数_main中引用
  • 函数声明是 int [] [8] 而它应该是 string example-name-for-the-compiler-to-know-what-to-look-for [] [8]跨度>
  • 非常感谢!!我让它工作。最后一件事,(我有没有提到谢谢?),我希望网格输出相同的字符串两次。例如,第一行会有一个随机生成的字符串:“Laura”、“Ghost”、“Drake”、“Rob”、“Rob”。第二行随机生成:“Drake”、“Nathan”、“Laura”、“Ghost”
  • @JRoustider 我想我明白,但我不确定......所以假设你得到 2,3,1,4。您想随机化该数组还是随机创建一个新数组?
  • 嗯,通过这个程序,我正在尝试制作一个益智配对游戏。除了随机化“卡片”背后的值外,我已经把所有东西都记下来了。在上面的代码中,我在一个多维数组中输出了板/网格,其面项为 [vgc],以及随机化字符串的函数(感谢您的帮助)。但现在我只需要随机化每个字符串中的两个并将值输入到板/网格中,以便玩家可以找到每个字符串中的两个并获得“匹配”。希望这可以澄清它。
【解决方案2】:

问题是,您正试图将 string 存储在您的 int 类型数组中。

for (int c = 0; c < 50; c++)
{
    characters[r][c] = vgc[i];  //<- **Here**
    cout << characters[r][c];
    i = i + 1;
}

【讨论】:

  • 那么我应该如何解决这个问题?