| View previous topic :: View next topic |
| Author |
Message |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Sun Jul 20, 2008 10:47 pm Post subject: C++, strlen and if/else |
|
|
| 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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jul 20, 2008 11:07 pm Post subject: |
|
|
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 |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Sun Jul 20, 2008 11:16 pm Post subject: |
|
|
One equal sign (=) is for assignment:
Two equal signs (==) are for statements:
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 |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Sun Jul 20, 2008 11:19 pm Post subject: |
|
|
I thought so, thats what you do in PHP  _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jul 20, 2008 11:20 pm Post subject: |
|
|
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 |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Sun Jul 20, 2008 11:23 pm Post subject: |
|
|
| 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  _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jul 20, 2008 11:26 pm Post subject: |
|
|
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 |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Sun Jul 20, 2008 11:26 pm Post subject: |
|
|
oh haha i see it... lol _________________
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Sun Jul 20, 2008 11:34 pm Post subject: |
|
|
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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Jul 21, 2008 6:24 am Post subject: |
|
|
| 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 |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Jul 21, 2008 11:40 am Post subject: |
|
|
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 |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Mon Jul 21, 2008 4:26 pm Post subject: |
|
|
it has been solved. _________________
|
|
| Back to top |
|
 |
|