logical - Code ignoring if statements - C++ -
i've been working on password generator college coursework, , 1 of parts involves creating 'complex' passwords, passwords nothing more strings of random characters, , user should able specify types of characters used. however, set of if statements control if function used don't activate based on values within uppertrue numbertrue , lowertrue, act if statement returns true, , function run. #include #include #include #include
int upper(), lower(), number(), symbol(); //initializing functions used generate ascii code int clength = 15; int pass[30]; int uppertrue = 0, numbertrue = 1, symboltrue = 0; int main() { srand (time(null)); //seed random generator int = 0; //counter int = 0; { = rand() % 4 + 1; //randomly decides type of character shown - probablity unweighted complex module if (which == 1) { pass[i] = lower(); //inserts code returned function array i++; } else if ((uppertrue == 1) && (which == 2)) { pass[i] = upper(); i++; } else if (numbertrue == 1 && == 3) { pass[i] = number(); i++; } else if (symboltrue == 1 && == 4) { pass[i] = symbol(); i++; } }while (i!=(clength+1)); //terminates loop when array complete std::string strpass; int x=0; { char tempchar; tempchar = pass[x]; std::cout << tempchar; x++; }while (x!=15); return 0; } int upper() //creates random number between range of ascii characters results in caps { return rand() % 65 + 26; } int number() //same upper numbers { return rand() % 48 + 9; } int lower() //same upper lower case { return rand() % 122 + 26; } int symbol() //same upper symbols (currently supporting few characters { return rand() % 63 + 6; }
if can point me in correct direction appreciated, seems it's logical error can't see logically wrong it. perhaps sort of quirk c++? (bearing in mind taught c , first thing i've done in c++) many (a comment said remove part i'd enter values uppertrue etc i've hardcoded values show problem instead)
your problem here:
int lower() // same upper lower case { return rand() % 122 + 26; }
it produce random number in range 26 ... 147. different range lower case characters. want this:
return rand() % ('z' - 'a' + 1) + 'a';
you should fix other functions in similar manner.
note worry code being able run on, example, mainframes using ebcdic character encoding: assumes a..z have continuous character codes.
Comments
Post a Comment