将随机字符串值放入int数组中
Placing Random String values into an int Array
我是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"的合适转换函数
我完全不知道如何解决这个问题。
不做 int characters [] [8];
,但做 string characters [] [8];
。在数组声明开始时指定的类型是数组将存储的信息的类型。和平。
问题是,您试图在int类型数组中存储字符串。
for (int c = 0; c < 50; c++)
{
characters[r][c] = vgc[i]; //<- **Here**
cout << characters[r][c];
i = i + 1;
}
相关文章:
- 按字符值访问int数组
- 将 int 数组转换为 std::vector<int*>
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 整数区间(或 int 数组)中每个数字的出现次数
- Int 数组到C++容器
- C++ 未初始化的本地(非全局)int 数组中的元素类型到底是什么?
- 如何检测将文本文件读入 int 数组的新行
- C++快速将 int 数组内容转储到文本文件中
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- 如何制作不允许重复值的 C++ int 数组循环?
- 如何将逗号分隔的文件读取为 2D int 数组?
- int 数组,但索引是字符?
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 将 int 数组转换为带有小数C++的双精度数组
- 将双精度值分配给 int 数组时的类型转换
- 已编辑 我如何将其拆分为 char 和 int 数组
- 从 C++ 中的 int ** 数组访问元素
- C++程序无法分配 int 数组?
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何通过头文件中的函数初始化 const int 数组?