0

Hello, I got my array sorted with different words in it

#include <iostream>
#include <ctime>
using namespace std;

int main()

{
cout << "this program will chose a name from a list randomly" << endl;
	char holdingArray[5][5] =
	{
		"John", 
		"Andy", 
		"Matt",
		"Jane"
	};

    char chosenBuffer[6]; // holds randon chosen word out of buffer

srand(time(0));


	return 0;
}

now I'm stuck.... maybe a little dumb but coulod anyone help? Basically I'm wanting the application to pick a word out of the array randomly and pop it into the buffer?

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

Take a look at the bottom of the forum; There's something there about random number selection you might find useful.

0

One way (certainly not a great one):

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;

int main()
{
   cout << "this program will chose a name from a list randomly" << endl;
   char name[][5] =
   {
      "John",
      "Andy",
      "Matt",
      "Jane"
   };
   srand(time(0));
   char pick [ sizeof *name / sizeof **name ];
   strcpy(pick, name[rand() % sizeof name / sizeof *name]);
   cout << pick << endl;

   return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.