这是我的一个简单游戏的代码。粘贴并尝试。
#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 行的末尾,一些名称、动作和位置重复。我如何让他们不重复并使用您将输入的每个名称?