Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++, strlen and if/else

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Localhost
I post too much
Reputation: 0

Joined: 28 Apr 2007
Posts: 3402

PostPosted: Sun Jul 20, 2008 10:47 pm    Post subject: C++, strlen and if/else Reply with quote

Code:

#include <iostream>
using namespace std;

int main()
{
   char x[255];
   int y = 32;
   int z = strlen(x);
   cout << "Enter text:";
   cin >> x;
   if (z = y) {
   cout << "Good job, i liek joo";
   }
   else{
   cout << "FUCKER!";
   }
}

No error, but it always returns Good job, i liek joo... even if i dont enter text that is 32 long.

_________________
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jul 20, 2008 11:07 pm    Post subject: Reply with quote

Try this.

Code:

#include <iostream>
using namespace std;

int main()
{
   char x[255];
   int y = 32;
   int z;
   cout << "Enter text:";
   cin >> x;
   z = strlen(x);
   if (z = y) {
   cout << "Good job, i liek joo";
   }
   else{
   cout << "FUCKER!";
   }
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sun Jul 20, 2008 11:12 pm    Post subject: Reply with quote

http://www.cplusplus.com/reference/clibrary/cstring/strlen.html


oib111, I tried your suggested code and got the same result as his.

_________________
Back to top
View user's profile Send private message
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Sun Jul 20, 2008 11:16 pm    Post subject: Reply with quote

One equal sign (=) is for assignment:
Code:
 if (z = y) {


Two equal signs (==) are for statements:
Code:
 if (z == y) {


By assigning the value of y to z, it will enter the statement block of code as long z (also y) is larger than 0. (0 is false, non-zero values are treated as true)
Back to top
View user's profile Send private message
Localhost
I post too much
Reputation: 0

Joined: 28 Apr 2007
Posts: 3402

PostPosted: Sun Jul 20, 2008 11:19 pm    Post subject: Reply with quote

I thought so, thats what you do in PHP Smile
_________________
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jul 20, 2008 11:20 pm    Post subject: Reply with quote

Oops. I just copy pasted his code and changed it so that he got the string length later, totally missed the = part =P
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Localhost
I post too much
Reputation: 0

Joined: 28 Apr 2007
Posts: 3402

PostPosted: Sun Jul 20, 2008 11:23 pm    Post subject: Reply with quote

Code:

#include <iostream>
using namespace std;

int main()
{
   char x[255];
   int y = 4;
   int z = strlen(x);
   cout << "Enter text:";
   cin >> x;
   if (z == y) {
   cout << "Good job, i liek joo";
   }
   else{
   cout << "FUCKER!";
   }
}


This only returns fucker Sad

_________________
Back to top
View user's profile Send private message MSN Messenger
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jul 20, 2008 11:26 pm    Post subject: Reply with quote

Dude...stop trying to get the strlen before you even get the string x_x

Code:

#include <iostream>
using namespace std;

int main()
{
   char x[255];
   int y = 4;
   cout << "Enter text:";
   cin >> x;
   int z = strlen(x);
   if (z == y) {
   cout << "Good job, i liek joo";
   }
   else{
   cout << "FUCKER!";
   }
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Localhost
I post too much
Reputation: 0

Joined: 28 Apr 2007
Posts: 3402

PostPosted: Sun Jul 20, 2008 11:26 pm    Post subject: Reply with quote

oh haha i see it... lol
_________________
Back to top
View user's profile Send private message MSN Messenger
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Sun Jul 20, 2008 11:34 pm    Post subject: Reply with quote

You also can't compare strings like that, it'll compare the addresses of both strings. (pointers to characters, you check the addresses, not values they point to, and even if you did, it would've checked only the first character)

call strcmp or simply make your own function.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Jul 21, 2008 6:24 am    Post subject: Reply with quote

Symbol wrote:
You also can't compare strings like that
He's only comparing the length..

Code:
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
  std::string s;
  int y = 4;
  std::cout << "Enter text:";
  std::getline( std::cin, s );
  if (s.length() == y)
    std::cout << "Good job, i liek joo";
  else
    std::cout << "FUCKER!";

  return EXIT_SUCCESS;
}
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Mon Jul 21, 2008 11:40 am    Post subject: Reply with quote

Code:
if (z = y) {


In C++you compare with '==' and you asign with '='. So in your code, you asigned z the value of y, so that is why it always returns true.

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
Localhost
I post too much
Reputation: 0

Joined: 28 Apr 2007
Posts: 3402

PostPosted: Mon Jul 21, 2008 4:26 pm    Post subject: Reply with quote

it has been solved.
_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites