Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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+中生成随机字符+;对于输入名称的三个提示中的每一个_C++_Random_Numbers_Srand - Fatal编程技术网

C++ 在C+中生成随机字符+;对于输入名称的三个提示中的每一个

C++ 在C+中生成随机字符+;对于输入名称的三个提示中的每一个,c++,random,numbers,srand,C++,Random,Numbers,Srand,此程序提示用户输入名称。然后它将使用两个while循环。一个while循环生成3个随机字母,后面是破折号,后面是另一个while循环生成3个随机数字。我可以让程序按要求执行三次 问题是,它将为输入的三个姓名中的每一个生成相同的三个随机数字和字母。我希望输入的每个姓名都打印一组唯一的字母和数字。它是否与srand()函数有关 还有一个问题是,在输入第二个名称后打印字符后添加破折号,在输入第三个名称后打印字符后添加两个破折号 #include <iostream> #include &l

此程序提示用户输入名称。然后它将使用两个while循环。一个while循环生成3个随机字母,后面是破折号,后面是另一个while循环生成3个随机数字。我可以让程序按要求执行三次

问题是,它将为输入的三个姓名中的每一个生成相同的三个随机数字和字母。我希望输入的每个姓名都打印一组唯一的字母和数字。它是否与srand()函数有关

还有一个问题是,在输入第二个名称后打印字符后添加破折号,在输入第三个名称后打印字符后添加两个破折号

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

int main() {
    int nameCount = 0;          // Hold the number of names entered by user
    string randomID;            // Used to concatenate the random ID for 3 names
    string name;                // To hold the 3 names entered by the user
    int numberOfCharacters = 0;
    int numberOfNumbers = 0;
    int a;
    srand(time(NULL));
    while(nameCount < 3) {
        cout << "\nEnter a name: ";
        getline(cin, name);
        while (numberOfCharacters < 3) {
            randomID += static_cast<int>('a') + rand() % 
                (static_cast<int>('z') - static_cast<int>('a') + 1);
            numberOfCharacters++;
        }
        randomID += "-";
        while (numberOfNumbers < 3) {
            randomID += static_cast<int>('1') + rand() %
                (static_cast<int>('1') - static_cast<int>('9') + 1);
            numberOfNumbers++;
        }
        cout << randomID;
        nameCount++;
    }
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
int namecont=0;//保留用户输入的名称数
string randomID;//用于连接3个名称的随机ID
string name;//保存用户输入的3个名称
int numberOfCharacters=0;
int numberOfNumbers=0;
INTA;
srand(时间(空));
而(名称计数<3){

不能将
randomID
设置为空,将
numberOfCharacters
设置为零,并在循环外仅将
numberOfNumbers
设置为零一次。相反,请执行以下操作:

int main() {
    int nameCount = 0;          // Hold the number of names entered by user
    string name;                // To hold the 3 names entered by the user
    int a;
    srand(time(NULL));
    while(nameCount < 3) {
        string randomID;            // Used to concatenate the random ID for 3 names
        int numberOfCharacters = 0;
        int numberOfNumbers = 0;
        cout << "\nEnter a name: ";
    ...
intmain(){
int namecont=0;//保留用户输入的名称数
string name;//保存用户输入的3个名称
INTA;
srand(时间(空));
而(名称计数<3){
string randomID;//用于连接3个名称的随机ID
int numberOfCharacters=0;
int numberOfNumbers=0;

没有问题,但您可以删除代码中的所有静态强制转换。
静态强制转换('a')+rand()%(静态强制转换('z')-static强制转换('a')+1)
'a'+rand()%完全相同('z'-'a'+1)
。有关为什么请参见:为什么要输入名称?为什么不在打印后和重新使用之前清除
随机ID
?这正是作业所需的
        randomID += static_cast<int>('1') + rand() %
            (static_cast<int>('1') - static_cast<int>('9') + 1);