Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;-while循环、字符串和整数_C++_String_Loops - Fatal编程技术网

C++ c++;-while循环、字符串和整数

C++ c++;-while循环、字符串和整数,c++,string,loops,C++,String,Loops,我想制作一个程序,用户输入几个名字,然后随机选取一个名字。但是,我不知道如何让绳子被挑起来。我想把每个字符串都分配给一个int,然后当选择int时,字符串也是如此。请帮帮我 #include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std; void randName()

我想制作一个程序,用户输入几个名字,然后随机选取一个名字。但是,我不知道如何让绳子被挑起来。我想把每个字符串都分配给一个int,然后当选择int时,字符串也是如此。请帮帮我

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <string>
    using namespace std;
    void randName()
    {
        string name;//the name of the entered person
        cout << "write the names of the people you want."; 
            cout << " When you are done, write done." << endl;
        int hold = 0;//holds the value of the number of people that were entered
        while(name!="done")
        {
            cin >> name;
            hold ++;
        }
        srand(time(0));
        rand()&hold;//calculates a random number
    }
    int main()
    {
        void randName();
        system("PAUSE");
    }
#包括
#包括
#包括
#包括
使用名称空间std;
void randName()
{
字符串名称;//输入的人员的名称

cout您可以使用
std::vector
来存储您的姓名,然后使用随机数按索引选择其中一个姓名。

您需要某种容器来存储姓名。
vector
非常适合于此

std::string RandName()
{
  std::string in;
  std::vector<std::string> nameList;

  cout << "write the names of the people you want."; 
  cout << " When you are done, write done." << endl;       

  cin >> in; // You'll want to do this first, otherwise the first entry could
             // be "none", and it will add it to the list.
  while(in != "done")
  {
    nameList.push_back(in);
    cin >> in;
  }    

  if (!nameList.empty())
  {
    srand(time(NULL)); // Don't see 0, you'll get the same entry every time.
    int index = rand() % nameList.size() - 1; // Random in range of list;

    return nameList[index];      
  }
  return "";
}
std::vector
void randName();
主要是
最烦人的解析
int main()
{
    std::string myRandomName = randName();
    system("PAUSE");
}