By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,942 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,942 IT Pros & Developers. It's quick & easy.

help please!

P: n/a
why does this not compile?

std::string test;
std::string tester="integer is a number";
int strlen = tester.length();
for(int i = 0; i<strlen-1;i++)
{
test[i]=tester[i];
if(test=="integer")
{cout<<"integer found!";}
}

I am trying to pull extract individual words from a sentence.

Nov 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
idikoko wrote:
why does this not compile?

std::string test;
std::string tester="integer is a number";
int strlen = tester.length();
for(int i = 0; i<strlen-1;i++)
{
test[i]=tester[i];
if(test=="integer")
{cout<<"integer found!";}
}

I am trying to pull extract individual words from a sentence.


I can't see any reason that would not compile. If you are getting
compilation errors then you should quote the error messages you get and
the lines they apply to.

However I can see why the code would not work. This line

test[i]=tester[i];

is wrong because test is a string with zero length, so test[i] is trying
to access a character in the string that doesn't exist. Instead you want

test.push_back(tester[i]);

push_back adds a character to the end of a string.

John
Nov 22 '05 #2

P: n/a
For future questions, it would help to include the error message your
compiler is giving you.

At a glance, I'd guess you might be redefining 'strlen', which is
already a standard function for finding the length of c-style strings.

Also, while it won't cause a compiler error, the line "test[i] =
tester[i]" is a serious logic error. The [] operator only provides
unchecked access and doesn't itself grow the string to provide storage
for random assignment. You might try test += test[i], which will grow
the test string dynamically by one character each time through the loop.

Nov 22 '05 #3

P: n/a
meagar wrote:
For future questions, it would help to include the error message your
compiler is giving you.

At a glance, I'd guess you might be redefining 'strlen', which is
already a standard function for finding the length of c-style strings.


That won't cause a compile error. The local name will just hide the
global one.

john
Nov 22 '05 #4

P: n/a
I got this new error when I used the push_back function.

'std::basic_string<_Elem,_Traits,_Ax>::push_back' : cannot convert
parameter 1 from 'std::string' to 'char'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

Nov 22 '05 #5

P: n/a
Ian
idikoko wrote:
I got this new error when I used the push_back function.

'std::basic_string<_Elem,_Traits,_Ax>::push_back' : cannot convert
parameter 1 from 'std::string' to 'char'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

Quote your code, did you type it correctly? Looks like you may have
left off the [i]

Ian
Nov 22 '05 #6

P: n/a
yeah thanks it worked thanks

Nov 22 '05 #7

P: n/a
John Harrison wrote:
meagar wrote:
For future questions, it would help to include the error message your
compiler is giving you.

At a glance, I'd guess you might be redefining 'strlen', which is
already a standard function for finding the length of c-style strings.


That won't cause a compile error. The local name will just hide the
global one.


I think the names of C standard library functions are all reserved
identifiers at any scope. This is in case the standard library
functions are implemented with macros, eg:

#define strlen(s) ( find_eos(s) - (s) )

I can't find exact wording to support this, but N869 7.1.4#1 says:
"Any function declared in a header may be additionally
implemented as a function-like macro defined in the header"

Nov 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.