Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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++_Arrays_String_Variables_Chars - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,arrays,string,variables,chars,C++,Arrays,String,Variables,Chars,现在我有了这个代码,它以用户输入确定的增量生成随机字母 #include <iostream> #include <string> #include <cstdlib> using namespace std; int sLength = 0; static const char alphanum[] = "0123456789" "!@#$%^&*" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqr

现在我有了这个代码,它以用户输入确定的增量生成随机字母

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int sLength = 0;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    cout << "What is the length of the string you wish to match?" << endl;
    cin >> sLength;
    while(true)
    {
        for (int x = 0; x < sLength; x++)
        {
            cout << genRandom();
        }
        cout << endl;
    }

}
#包括
#包括
#包括
使用名称空间std;
int sLength=0;
静态常量字符alphanum[]=
"0123456789"
"!@#$%^&*"
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
“abcdefghijklmnopqrstuvwxyz”;
int stringLength=sizeof(alphanum)-1;
char genRandom()
{
返回alphanum[rand()%stringLength];
}
int main()
{
coutslength;
while(true)
{
对于(int x=0;x
string s(sLength, ' ');
while(true)
之前,更改

cout << genRandom();
在循环中,删除
cout,只需添加

string s(sLength, ' ');
while(true)
之前,更改

cout << genRandom();
在你的循环中,删除
cout那么,这个怎么样

    std::string s;
    for (int x = 0; x < sLength; x++)
    {
        s.push_back(genRandom());
    }
std::字符串s;
对于(int x=0;x
这个怎么样

    std::string s;
    for (int x = 0; x < sLength; x++)
    {
        s.push_back(genRandom());
    }
std::字符串s;
对于(int x=0;x
#包括
#包括
// ...
int main()
{
srand(time(0));//别忘了我
while(true){
coutslength;
字符串r(长度为“”);
生成(r.begin(),r.end(),genRandom);
不能包含
#包括
// ...
int main()
{
srand(time(0));//别忘了我
while(true){
coutslength;
字符串r(长度为“”);
生成(r.begin(),r.end(),genRandom);

除了字符串s(SLENGHT)之外,这行得通吗?它会弹出一个错误。从int到const char的转换无效。@西斯托伦:我假设它的工作方式类似于
向量(可以这样构造)。我会修复它。最好是做s.reserve(SLENGHT)-这样你就不必写每个位置两次了。@kotlinski:我写元素时使用的是
[]
而不是
向后推
,因此我需要
调整大小
而不是
保留
;我认为
调整大小
也会写所有元素。这可以在字符串s(sLength)之外工作;它弹出一个错误。从int到const char的转换无效。@Cistoran:我假设它的工作方式类似于
vector
(可以这样构造)。我会解决它。最好是做s.reserve(sLength)-这样你就不必每一个位置都写两次。@kotlinski:我正在使用
[]
而不是
向后推
,因此我需要
调整大小
而不是
保留
;我认为
调整大小
也会写入所有元素。