Before I get started with this question I would like to point out that this is a small app I am creating to not only learn more C++ coding but also to choose who drives the fifth day in my carpool group for college.

I am creating an application in C++ that lets the user enter some people's names and it chooses one of those people at random every time you enter the text "Go" (minus the ""). I have all the text printed out and I also have it where it stores the people as a string but I can not figure out how to choose one of the strings at random. Here is my code so far:

#include <cstdlib>
#include <iostream>

using namespace std;

string a = "Jimmy";
string b = "Nathan";
string c = "Cochran";
string d = "Harris";

int main(int argc, char *argv[])
{
    cout<<"Current Car Pool Riders/Drivers\n";
    cout<<endl;
    cout<<"1.) Jimmy\n";
    cout<<"2.) Nathan\n";
    cout<<"3.) Cochran\n";
    cout<<"4.) Harris\n";
    cout<<endl;
    cout<<"Type Go and Press Enter to See Who Drives the Fifth Day\n";
    cout<<endl;
    cin>>a;
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Please note I am an ABSOLUTE beginner at C++ and decided to pick it up because my friends are taking it in college. But I am very proficient with Visual Basic and C Sharp.

Thanks for the help and I hope to get a reply very soon because I need this to see who drives to college tomorrow :)

I can't seem to get this to work. I am sorry for the bother but as I said, I am an ABSOLUTE beginner when it comes to C++. Also I need the user to click enter and it will randomly display one of the strings. If you could, could you use the code I provided that I have and add the code you gave me to it and show me the revised code? Thanks.

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;
}

Thanks you for your help but I am still running into some issues. I am using the following code but it is always choosing the person in the top slot. It is like it is not shuffling the people.

#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector> 
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    
    vector< string > drivers;
    drivers.push_back("Jimmy");
    drivers.push_back("Nathan");
    drivers.push_back("Cochran");
    drivers.push_back("Harris");
 
    //randomly shuffle the vector
    std::random_shuffle( drivers.begin(), drivers.end() );
 
    string pick = drivers[0];
    cout << "The Person Driving the Extra Day is - " << pick << endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

Here is my newest code and I also removed return 0; because it kept opening and then closing automatically.

#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector> 
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    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;
}

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;
}
Comments
Worked like a charm :)

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 number x from 0 to 3
4) Display the xth name from the array.

WaltP, can you show an example code for me so I can better see what you are speaking of?

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] << ".";

@firstperson: Why would absolute beginner, that does not know how to use rand(), use vectors? I was programming C++ for a year and a half now, and I just learned what vectors are a month ago!

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't have 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:

  1. 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.
  2. 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.

Comments
Agreed. 100%

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."

This question has already been answered. Start a new discussion instead.