Need help with this c++ program

shiftyape

Honorable
Feb 23, 2013
48
0
10,580
#include <iostream>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;

void random();

char thin, chin;
char pass[36] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
int num, z, time1, r;

int main() {

cout << "This is the random password generator. How many characters do you want the password to be?" << endl;
cin >> time1;

cout << endl;

random();

cout << endl;
cout << endl;


system("PAUSE");
return 0;
}

void random() {

srand(time(NULL));
while (time1 > 0) {

time1--;

z = rand() % 36 + 1;

chin = pass[z];

if (pass[z] == 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z') {

r = rand() % 2;
if (r == 1) { //convert to upper
thin = toupper(chin);
cout << thin;
}
else cout << chin; //display letter uncapped
}
else cout << chin; //display number
}
}



for some reason, when i run this, spaces appear in the password. how do i fix this?
example:
2H43fdas fjdkas3423hjh4h23GHJFS65 fjdksa
what is want is
2H43fdasfjdkas3423hjh4h23GHJFS65fjdksa
 
Solution

Ijack

Distinguished
You seem to be still treating the array as if the subscripts run from 1 to 36 rather than 0 to 35. I can't see how you ever get an 'a' in your output. I suspect the space may be coming from the (non-existant) array element pass[36].
 

Pinhedd

Distinguished
Moderator




Hi,

There are several things wrong with your program.

The cause of the extraneous space is most likely what Ijack pointed out. You have created a statically allocated 36 element array. This array has indicies 0 through 35. Your index generator generates indicies 1 through 36. At the most extreme you will be accessing index 36 which leads to memory that is outside of the bounds of the array. Most of the time this will still be a valid memory access, and will not cause a compilation error or runtime error but it will not be the memory that you are looking for (see what I did there?) and will more often then not yield incorrect and often non-deterministic results. It could be another variable in your program, it could be allocated but uninitialized data, or it could result in an access violation. In any case, avoid doing that at all costs.

Second, this statement below does not do what you think it does
C++:
if (pass[z] == 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 
	't', 'u', 'v', 'w', 'x', 'y', 'z') {
I presume that you wish to check if the randomly generated value corresponds to a lower case latin character. The code above doesn't do that.

In C style programming languages an if() control statement will proceed if the enclosed expression evaluates to true.
In C style programming languages, any non-zero number is considered true.
In C style programming languages, single-quotations expand characters to an character or integer type (where Unicode is supported) which contains its respective character mapping.
In the C and C++ programming languages there exists a little-known comma operator which can be used in expressions to provide some neat functionality. The comma operator separates expressions, evaluating each from left to right, and returns the result of the last evaluated expression.
Your conditional statement evaluates a number of constants, which have no effect at all and will most likely be optimized out by the compiler, and then evaluates a final constant 'z' for the conditional statement. 'z' evaluates to 0x7A (122 decimal). Ergo, the above is logically equivalent to the following:
C++:
if ('z') {
and
C++:
if (122) {

That if statement will always be executed regardless of what happens, and will most likely be optimized out by the compiler.

You could fix it, but there's no point. The toupper() function already checks the input for validity and will only convert a lower case character which has an upper case mapping. If it doesn't, it does nothing. Pass it all of the numbers and upper case characters that you want.
 
Solution

shiftyape

Honorable
Feb 23, 2013
48
0
10,580


best reply ever thanks a ton man. worked fine, program is now working. thanks