length of a string

C

cdolphin88

Hi,

I'm reading from a .dat file and I want to know the length of the
string .

I' using the stringRef.length () but the compiler said there was an
error

`stringRef' undeclared (first use this function)

Is there any function in C++ to determine the lenght of the string??


Cheers!
 
T

TB

(e-mail address removed) skrev:
Hi,

I'm reading from a .dat file and I want to know the length of the
string .

I' using the stringRef.length () but the compiler said there was an
error

`stringRef' undeclared (first use this function)

Is there any function in C++ to determine the lenght of the string??

? Read the error message!
 
J

Jakob Bieling

Hi,

I'm reading from a .dat file and I want to know the length of the
string .

I' using the stringRef.length () but the compiler said there was an
error

`stringRef' undeclared (first use this function)

Is there any function in C++ to determine the lenght of the string??

Your problem is not a missing function (it is there, trust me), but
a missing string. Cannot determine the length of something that is not
there, can you?

If you still cannot figure out what you are doing wrong, try:

#include <string>

int main ()
{
std::string s = "Hello";
s.length ();
}

hth
 
R

Rolf Magnus

Hi,

I'm reading from a .dat file and I want to know the length of the
string .

What string? What is a .dat file?
I' using the stringRef.length () but the compiler said there was an
error

`stringRef' undeclared (first use this function)

Well, then I guess you forgot to declare stringRef.
Is there any function in C++ to determine the lenght of the string??

Yes, the lenght() member function (if you are referring to std::string).
 
J

Jim Langston

Hi,

I'm reading from a .dat file and I want to know the length of the
string .

I' using the stringRef.length () but the compiler said there was an
error

`stringRef' undeclared (first use this function)

Is there any function in C++ to determine the lenght of the string??

Yes, it is indeed .length() for std::string

It sounds more like your std::string stringRef is undefined. Most likely
you have a typo and misstyped the name of the function. Are you sure it
shouldn't be StringRef or soemthing? Compare where you declared stringRef
and how you're using it.

stringRef should be declared something like:
std::string stringRef;
 
B

BobR

Rolf Magnus wrote in message ...
"""""""
I' using the stringRef.length () but the compiler said there was an
error
`stringRef' undeclared (first use this function)

Well, then I guess you forgot to declare stringRef.
Is there any function in C++ to determine the lenght of the string??

Yes, the lenght() member function (if you are referring to std::string).
"""""""

Don't forget to define it first:

#include <string> // C++
#include <iostream>
#include <ostream> // std::endl

size_t lenght(std::string const &Tstr){
return Tstr.size();
// or: return Tstr.length();
}

int main(){
std::string stringRef( "Yes, the lenght() member" );
std::cout<<" size of stringRef is "<<lenght( stringRef )<<std::endl;
return 0;
}

// output: size of stringRef is 24
 
H

Howard

BobR said:
Rolf Magnus wrote in message ...
"""""""


Well, then I guess you forgot to declare stringRef.


Yes, the lenght() member function (if you are referring to std::string).
"""""""

Don't forget to define it first:

#include <string> // C++
#include <iostream>
#include <ostream> // std::endl

size_t lenght(std::string const &Tstr){
return Tstr.size();
// or: return Tstr.length();
}

int main(){
std::string stringRef( "Yes, the lenght() member" );
std::cout<<" size of stringRef is "<<lenght( stringRef )<<std::endl;
return 0;
}

// output: size of stringRef is 24

May I assume we're actually talking about the "length" function here, not
the "lenght" function? :)
 
B

BobR

Howard wrote in message ...
May I assume we're actually talking about the "length" function here, not
the "lenght" function? :)

Wal, ewe dint want me two tel hymn he spelt sumthin rong didja?
<G>

Anyway, maybe the OP will see something that will help them. (...and I,
hopefully, at least got a chuckle out of somebody).

The question is, does '*.length()' return '*.size()', or does '*.size()'
return '*.length()'?
Neither! Looking at my (MinGW GCC) basic_string.h:

// Capacity:
size_type size() const { return _M_rep()->_M_length; }
size_type length() const { return _M_rep()->_M_length; }

So, either one is as good as the other. (no extra 'call overhead')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top