Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Problem | std::string length gives Junk value in expression (http://www.velocityreviews.com/forums/t562592-problem-std-string-length-gives-junk-value-in-expression.html)

 Pradeep 12-20-2007 01:32 PM

Problem | std::string length gives Junk value in expression

Hi All,

I am facing an issue where length method of std::string class gives a
junk value when used in a expression.

Here's an example. The code should not go into the for loop but it
does because the value of j-str.length comes out to be some junk value

int j = 12;
std::string
str("0123456789012"); //
of length 13
std::cout << "str length" << str.length() <<
std::endl; // gives 13
std::cout << " j - str.length" << (j- (str.length())) <<
std::endl; // gives junk value
for(int i=0; i < j - str.length() ;i+
+) // so it goes
inside the loop
//
while it should not for 12-13
//
= -1
{
std::cout << "Show hte problem" << std::endl;
}

However it starts to work if instead of the expresssion I use a
variable
int x = j - str.length.

Can somebody help me out and tell me the reason for this.

 Tim Love 12-20-2007 02:19 PM

Re: Problem | std::string length gives Junk value in expression

>Hi All,

>I am facing an issue where length method of std::string class gives a
>junk value when used in a expression.

string isn't the problem. Try this

#include <iostream>
int main() {
unsigned int k=13;
int j = 12;
std::cout << "12 - 13 =" << j - k << std::endl;;
}

and have a look at what length() returns.

 All times are GMT. The time now is 08:19 AM.