سؤال

I am pretty newish at c++ and was wondering how I can place values randomly into an int array.What I want to do is use this function to place into a grid randomly for the purpose of creating a memory matching game.

#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;
  }
}

It also gives me an error for one of the variableNames (vgc) saying

1 IntelliSense: no suitable conversion function from "std::string" to "int" exist

I am completely stumped on how to fix this solution.

هل كانت مفيدة؟

المحلول

Dont do int characters [] [8]; , but string characters [] [8]; . The type you specify at the start of the array declaration is the type of info the array will store. Peace.

نصائح أخرى

The problem is, you are trying to store string in your int type array.

for (int c = 0; c < 50; c++)
{
    characters[r][c] = vgc[i];  //<- **Here**
    cout << characters[r][c];
    i = i + 1;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top