다음은 간단한 게임을위한 코드입니다. 그것을 붙여 넣으십시오.배열을 반복하지 않게 만드는 방법은 무엇입니까?
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main (void)
{
string g[4],
b[4],
a[4],
l[4];
srand((unsigned int)time(0));
cout << "Welcome!\n\n";
cout << "Type 4 girl names.\n";
for (int gi = 0; gi < 4; gi++)
cin >> g[gi];
cout << "Type 4 boy names.\n";
for (int bi = 0; bi < 4; bi++)
cin >> b[bi];
cout << "\nWhat they do (enter 4 actions)?\n";
for (int ai = 0; ai < 4; ai++)
getline(cin, a[ai]);
cout << "\nWhere is happening (enter 4 locations)?\n";
for (int li = 0; li < 4; li++)
getline(cin, l[li]);
for (int c = 0; c < 4; c++)
cout << g[rand() % 4] << " and " << b[rand() % 4] << " are " << a[rand() % 4] << " from a " << l[rand() % 4] << endl;
return (0);
}
4 줄의 끝에 일부 이름, 동작 및 위치가 반복됩니다. 당신이 입력 할 모든 이름을 반복하여 사용하지 않게하려면 어떻게해야합니까?
무작위로 선택하기 때문에 반복됩니다. 따라서 반복을 중지하려면 무작위로 선택하지 마십시오. – Jon
무작위로 이름, 동작 또는 위치를 선택하는 임의의 기능이 필요합니다. 여기에 문제가 있습니다. – blackseed32
해결 방법은 배열의 [random_shuffle] (http://en.cppreference.com/w/cpp/algorithm/random_shuffle)입니다. – neam