Random names using an array
Oct 1, 2012 at 8:27pm UTC
Im currently using a switch statement for the names but i think an array would be easier but how to i get the program to randomly get a name from the array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
void randNames()
{
int random;
srand(time(NULL));
for (int i = 0; i < 7; i++)
{
random = rand() % 7;
}
switch (random)
{
case 0:
cout << "Jerry" << endl;
break ;
case 1:
cout << "Mike" << endl;
break ;
case 2:
cout << "Phil" << endl;
break ;
case 3:
cout << "Robert" << endl;
break ;
case 4:
cout << "Allen" << endl;
break ;
case 5:
cout << "Alex" << endl;
break ;
case 6:
cout << "Tim" << endl;
break ;
}
}
Oct 1, 2012 at 8:39pm UTC
Use the randomly generated number as the index for the array that contains the names:
1 2 3 4 5 6 7
std::string names[7] =
{
// ...
};
srand(time(0x0));
std::cout << names[(rand( ) % 6)];
Wazzak
Oct 1, 2012 at 9:04pm UTC
why is there 0x0 instead of NULL?
Oct 1, 2012 at 9:07pm UTC
It's the same thing. I use 0x0 rather than "NULL ".
Wazzak
Oct 1, 2012 at 9:13pm UTC
Ok i tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
struct Vars
{
void Game();
string randNames();
long long int money;
int prisoners;
string playerName;
string prisonName;
};
int main()
{
int choice;
Vars v;
cout << "1) New" << endl;
cout << "2) Load\n" << endl;
cin >> choice;
if (choice == 1)
{
v.money = 50000;
v.prisoners = 0;
ofstream file;
file.open("prison.txt" );
cin.ignore(1000, '\n' );
cout << "Hello please enter your name" << endl;
getline(cin, v.playerName);
file << v.playerName << endl;
cout << "\n" ;
cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
getline(cin, v.prisonName);
file << v.prisonName << endl;
cout << "\n" ;
cout << "Ok thank you lets start the game" << endl;
cin.get();
file << v.money << endl;
file << v.prisoners << endl;
file.close();
v.Game();
}
else if (choice == 2)
{
ifstream file;
file.open("prison.txt" );
file >> v.playerName;
file >> v.prisonName;
file >> v.money;
file >> v.prisoners;
file.close();
v.Game();
}
}
void Vars::Game()
{
int choice;
cout << "Main Menu\n" << endl;
cout << "What do you want to do?\n" << endl;
cout << "1) View list of prisoners" << endl;
cout << "2) View list of executed prisoners" << endl;
cout << "3) View prison funds and spending" << endl;
cout << randNames() << endl;
}
string Vars::randNames()
{
int random;
string names[7] =
{
"Jerry" ,
"Mike" ,
"Phil" ,
"Robert" ,
"Allen" ,
"Alex" ,
"Tim"
};
srand(time(0x0));
cout << names[rand() % 7];
}
void randCrimes()
{
int random;
srand(time(NULL));
for (int i = 0; i < 7; i++)
{
random = rand() % 7;
}
switch (random)
{
case 0:
cout << "Rape" << endl;
break ;
case 1:
cout << "Grand Theft" << endl;
break ;
case 2:
cout << "Murder" << endl;
break ;
case 3:
cout << "Child Abuse" << endl;
break ;
case 4:
cout << "Petty Theft" << endl;
break ;
case 5:
cout << "Assault With a Deadly Weapon" << endl;
break ;
case 6:
cout << "" << endl;
break ;
}
}
And when i went to my code, the computer started beeping and screwed up, what did i do wrong?
Oct 1, 2012 at 10:26pm UTC
randNames() has no return statement.
Oct 1, 2012 at 10:29pm UTC
So return names; ??
Oct 1, 2012 at 10:49pm UTC
Nevermind i got it thanks.
Oct 1, 2012 at 10:50pm UTC
0x0 is hexadecimal for 0. NULL is a #define for 0.
Oct 2, 2012 at 2:52am UTC
Do not call srand before every call to rand.
Only call srand once at program startup.
Topic archived. No new replies allowed.