473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

help please!

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
7 1192
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
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
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
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
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
yeah thanks it worked thanks

Nov 22 '05 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
1
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the ...
0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
12
by: Christo | last post by:
borland c++ 5.01 character constant must be one or two characters long get this when compiling my first c++ program can anyone out there help? it is highlighting this line as the problem ...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
17
by: JT | last post by:
Help me the following C++ question: Write a program to help a local bookshop automate its billing system. The program should do the following: (a)Let the user enter the ISBN, the system will...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
2
by: =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post by:
I'm creating a doc project for my c# program. I've done this before but this time sonething is wrong. I build my doc project and is succeeds but when I open the help file, there is no documentation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.