If its to learn C++, then I suggest you to do this.
1) In a file list all of the participants name
2) From your program, read/store the participants name
3) Pick a random name and output
It will be easier if your program just shows the random person's name instead of having the user enter 'Go'
Alternatively, if you don't have a lot of names, then it might be easier to hard-code the names in a vector. And then shuffle it and pick a random name. Judging from your post it looks like you only have a few names. In that case, it might be easier to do hardcode it.
Here is a sample code that you can work with.
#include <iostream>
#include <algorithm> //to randomly shuffle
#include <vector> //our container
#include <string>
using namespace std;
int main(){
vector< string > fruits;
fruits.push_back("apple"); //add a fruit to our container
fruits.push_back("orange");
fruits.push_back("pear");
//randomly shuffle the vector
std::random_shuffle( fruits.begin(), fruits.end() ); //reorders the container in random order
string pick = fruits[0]; //pick the first element, could be any since the container was 'shuffled'
cout << "Today, you should eat " << pick << " for breakfast " << endl;
return 0;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Oh right, you need to seed first.
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
int main(int argc, char *argv[])
{
srand( time(0) ); //seed
cout<<"Current Car Pool Riders/Drivers\n";
cout<<endl;
cout<<"Monday - Nathan\n";
cout<<"Tuesday - Jimmy\n";
cout<<"Wednesday - Cochran\n";
cout<<"Thursday - Harris\n";
cout<<endl;
vector< string > drivers;
drivers.push_back("Jimmy");
drivers.push_back("Nathan");
drivers.push_back("Cochran");
drivers.push_back("Harris");
std::random_shuffle( drivers.begin(), drivers.end() );
string pick = drivers[0];
cout<<"The Person Driving this Friday is - " << pick << endl;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Forget the vector. Just
1) Create a string array with all the names in it.
2) Use srand() once to initialize the random function.
3) call rand() to get your random numberx from 0 to 3
4) Display the xth name from the array.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
To follow WaltP's adive you could simply do
srand(unsigned(time(0)));
string names[] = {"Jim", "Tom", "Jeff", "Larry"};
cout << "The person that should drive is: " << names[rand % 4] << ".";
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Why would absolute beginner, that does not know how to use rand(), use vectors?
What's wrong with vectors? I assume you're suggesting that arrays be used instead, which is silly. C++ doesn'thave to be learned from the bottom up. In fact, learning C++ with an eye toward realistic code (using available libraries to make things easier) is generally a better approach for two reasons:Beginners can start writing powerful code much sooner. There's a lot of prerequisite knowledge and experience required to simulate what std::vector does versus simply using std::vector from day one.
Many of the lower level aspects of C++ are both unnecessary and also unnecessarily complex for a beginner.
I was programming C++ for a year and a half now, and I just learned what vectors are a month ago!
So? All this implies is that you've ignored the more useful parts of the standard library for far too long.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
What ^she^ said... with an emphasis on " All this implies is that you've ignored the more useful parts of the standard library for far too long."
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608