将随机字符串值放入int数组中

时间:2014-05-12 10:08:04

标签: c++ arrays function for-loop

我对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智能感知:从“std :: string”到“int”不存在合适的转换函数

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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