-
string-keygen type app
hi, i want to make a kind of keygen using strings..
this app accepts name then never comes back.
Code:
#include <iostream.h>
#include <string.h>
void main(void)
{
char string[50];
char retstring[50];
int length,x;
cout<<"Enter your name: ";
cin.getline(string,50,'\n');
length=strlen(string);
for (x=0;x=length;x++)
{
string[x]=string[x]+1;
}
cout<<"\nYour password is: "<<string;
}
whats up with that?
-
>>for (x=0;x=length;x++)
Are you sure you want assignment there?
-
-
for (x=0;x=length;x++)
The equal sign is an assignmwnt operator; it is used to set a variable or array to a value.
You want the double equal sign, which tests if the two are equal.
for (x=0; x==length; x++)
-
>>it is used to set a variable or array to a value.
I disagree... If you try setting an array using the equals sign, you'll just be changing the address it points to, which is pointless (haha pun... *cough cough*), and besides which it will most likely give you a compiler error (MSVC does, haven't tested any others). The only time you can use = to set an array is at initialization, using curly braces to indicate that you're specifying a list of elements.
As a side note, I prefer to use ++x instead of x++ when it can be used interchangeably, because it is *potentially* faster, by 1/9999 ms :)
Aside from that, I agree with everything that's been said.
-
>If you try setting an array using the equals sign, you'll just be changing the address it points to
Well if the array was actually declared as an array, an assignment to it is illegal because an array is only converted to a pointer to the first element in an expression (excluding sizeof and address-of (&) being performed on the array), a function argument or a subscript operation. An array name is not a modifiable lvalue, thus cannot be assigned to.
If the array is simulated dynamically with a pointer or points to an array declared as an array then what you said is true.
>it will most likely give you a compiler error
It definitely will since it is a constraint violation.
>because it is *potentially* faster
Fair enough. Though with built-in types you should get a better compiler if ++i is noticeably faster than i++ in all but the most pedantic of tests that would never reasonably arise in any real code.
>by 1/9999 ms
And how many times would that statement have to be iterated over before the speed difference actually makes a difference? :rolleyes:
-
>>an array is only converted to a pointer to the first element in an expression (excluding sizeof and address-of (&) being performed on the array), a function argument or a subscript operation.
Ah wow, never knew that... all my books always just said that an array is a pointer :eek:
>>And how many times would that statement have to be iterated over before the speed difference actually makes a difference?
Er... if you had a [(really)e99]^20 heavy-duty server program? :p
-
for statements
Hi,
After all the posts that I bet didn't really help you, here is why it didn't work.
The for-statement works like this
for (<counter var definition>;<loop condition>;<increment/decrement>)
The <loop condition> expression must be FALSE in order for the for loop to exit, or in other words, the for-loop will continue until the condition is FALSE.
In your code, you did an assignment, which when evaluated, is always (for the purposes of this discussion) TRUE. So, you have created yourself a nice endless loop. ;)
What you want to do, based on what you said and what I think your goal is, is to change that line to
(int i=0; i < length ; i++)
or, in pseudocode,
"start with i equal to zero, and while it's less than length, do what's in brackets below, then increase i, and come back here."
I hope that helps a bit more than the other stuff posted above here, which may all be true, but I think is not very helpful to you.
Good luck!
:)
-
nice work gice. 100x thanks