Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++ 代码忽略if语句-C++;_C++ - Fatal编程技术网

C++ 代码忽略if语句-C++;

C++ 代码忽略if语句-C++;,c++,C++,我一直在为我的大学课程设计一个密码生成器,其中一个部分涉及到创建“复杂”密码,即只不过是随机字符字符串的密码,用户应该能够指定所使用的字符类型。但是,控制函数是否被使用的if语句集不会根据uppertrue numbertrue和lowertrue中的值激活,它们的作用就像语句返回true一样,因此函数始终运行。 #包括 #包括 #包括 #包括 int upper(), lower(), number(), symbol(); //initializing functions to be

我一直在为我的大学课程设计一个密码生成器,其中一个部分涉及到创建“复杂”密码,即只不过是随机字符字符串的密码,用户应该能够指定所使用的字符类型。但是,控制函数是否被使用的if语句集不会根据uppertrue numbertrue和lowertrue中的值激活,它们的作用就像语句返回true一样,因此函数始终运行。 #包括 #包括 #包括 #包括

int upper(), lower(), number(), symbol();   //initializing functions to be    used to generate the ascii code
int clength = 15;
int pass[30];  
int uppertrue = 0, numbertrue = 1, symboltrue = 0;
int main()
{
    srand (time(NULL));                                                        //seed random generator
    int i = 0;                                                              //counter
    int which = 0;
    do
    {
        which = rand() % 4 + 1;     //randomly decides which type of character will be shown - probablity is unweighted for complex module
        if (which == 1)
        {
            pass[i] = lower();      //inserts the code returned  by the function into the array
            i++;
        }
        else if ((uppertrue == 1) && (which == 2))
        {
            pass[i] = upper();
            i++;
        }
        else if (numbertrue == 1 && which == 3)
        {
            pass[i] = number();
            i++;
        }
        else if (symboltrue == 1 && which == 4)
        {
            pass[i] = symbol();
            i++;
        }
    }while (i!=(clength+1));        //terminates loop when the array is complete
    std::string strpass;
    int x=0;
    do
    {
        char tempchar;
        tempchar = pass[x];
        std::cout << tempchar;
        x++;
    }while (x!=15);
    return 0;
}

int upper()     //creates random number between the range of ascii characters that results in caps
{
    return rand() % 65 + 26;

}

int number()    //same as upper but for numbers
{
    return rand() % 48 + 9;
}

int lower()     //same as upper but for lower case
{
    return rand() % 122 + 26;
}

int symbol()    //same as upper but for symbols (currently only supporting a few characters
{
    return rand() % 63 + 6;
}
int-upper(),lower(),number(),symbol()//初始化用于生成ascii代码的函数
int-clength=15;
国际通行证[30];
int uppertrue=0,numbertrue=1,symboltrue=0;
int main()
{
srand(time(NULL));//种子随机生成器
int i=0;//计数器
int=0;
做
{
which=rand()%4+1;//随机决定将显示哪种类型的字符-复杂模块的概率未加权
if(其中=1)
{
pass[i]=lower();//将函数返回的代码插入数组
i++;
}
else如果((uppertrue==1)和&(which==2))
{
pass[i]=upper();
i++;
}
else if(numbertrue==1&&which==3)
{
pass[i]=number();
i++;
}
else if(symboltrue==1&&which==4)
{
pass[i]=symbol();
i++;
}
}while(i!=(clength+1));//在数组完成时终止循环
std::字符串strpass;
int x=0;
做
{
char tempchar;
tempchar=pass[x];
std::cout您的问题在这里:

int lower()     // same as upper but for lower case
{
    return rand() % 122 + 26;
}
它将生成26…147范围内的随机数。这与小写字符的范围完全不同。您希望:

    return rand() % ('z' - 'a' + 1) + 'a';
您应该以类似的方式修复其他函数

请注意,那些担心自己的代码能够在使用EBCDIC字符编码的大型机上运行的人:这假设a..z具有连续的字符代码。

具体的问题是,在随机返回各种字符的函数中存在错误

C++标准对与字符相关联的数值是有意模糊的,精确映射是实现的,该方案称为编码。 虽然ASCII编码很常见,但它绝不是通用的,因此为了实现可移植性,除非您确实需要,否则最好不要对您的平台进行假设

因此,您确实应该在以下行中重铸
lower

char lower
{
    const char* s = "abcdefghijklmnopqrstuvwxyz";
    return s[rand() % 26];
}
这是真正可移植的。我还随意更改了你的函数返回类型

您应该为
上限
执行类似操作。您的
符号
函数也将以类似方式退出

我也希望对<代码>编号<代码>采用同样的方法,但是这里C++的标准对数字表示了一些:编码<强>必须< /强>将字符<代码> 0 < /C> > < <代码> 9 > /代码>为一个连续的块,按此顺序,因此语句

return rand()%('9'-'0'+1)+'0';


是可移植的。最后,您可以使用
static char[]s=“abc…z”
(sizeof(s)-1)
代替硬编码26。这是一项非常先进的技术,初学者不太清楚,但请随着编程技能的发展进行研究。

请稍微减少一点问题。对于初学者,摆脱
std::cin
调用,用硬编码输入代替,以尽可能简洁地演示问题。看,这不是你要问的问题,但是你需要做
rand()%26+65
rand()%26+97
(不是122)。除了(不正确的)之外,你还可以使用
rand()%('Z'-'A'+1)+'A'
rand()魔法数字,
lower()
upper()
number()
symbol()
的代码不可移植。
number()
易于移植实现:
return rand()%10+'0';
。其他代码应使用
char
char lc[]=“abcdefghjjjklnopkrstuvxyz”数组实现;
然后
返回lc[rand()%26]
。我想你的代码编辑打破了这个问题。现在
uppertrue
numbertrue
symboltrue
从来都不是真的,而整个代码也因此而变得混乱……如果它们总是
false
,那么只需删除它们和现在永远无法执行的代码。或者如果这段代码就是重点对问题进行编辑,使其为真。你所说的“开始提问时输入1”是什么意思?您已经删除了程序中要求输入的部分。多谢各位,我在找一个完全错误的地方,试图找到最初对此进行否决的修正。您确实充分否认了编码,只要OP知道可移植性限制,这是一个很好的答案。这种方法是唯一的敏感方法一个用于
symbol()
函数,尤其是。