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);
}
At the end in the 4 lines some of the names, actions and locations repeat. How do I make them to not repeat and use every name that you will enter? do I need random_shuffle? How can I integrate it in my code?
#include <algorithm>
// after entering the data ...
random_shuffle(g,g+4);
random_shuffle(b,b+4);
random_shuffle(a,a+4);
random_shuffle(l,l+4);
for (int c = 0; c < 4; c++)
{
cout << g[c] << " and " << b[c] << " are " << a[c] << " from a " << l[c] << endl;
}
Also, there is a bug when entering the data. It happens when you use operator >>
followed by getline() [ a google search should describe the problem in detail ].
Between the operator >> and the getline(), use the ignore() function:
Bookmarks