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?

Recommended Answers

All 2 Replies

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.