Re: string comparing problem
Summary of what you have ...
Code:
char * string_a = new char[loaded_values.size() + 1];
char * string_b = new char[loaded_values.size() + 1];
if(string_a==string_b)
You should not compare c-style strings using ==
You should use strcmp ...
Code:
if (strcmp(string_a,string_b) == 0)
Also:
You have global std::string variables named string_a and string_b , and you
also have local char* variables with the same name. This can be confusing.
Re: string comparing problem
Quote:
Originally Posted by
RichardsPeter39yahoo
Perhaps my problem arises when I rename the string from loaded_values to string_a
and string_b, as per this code segment here:
char * string_a = new char[loaded_values.size() + 1];
std::copy(loaded_values.begin(), loaded_values.end(), string_a);
In addition to what Philp stated, why are you using char pointers when you are using std::string everywhere else? Your program has a memory leak due to you using new[] without a proper call to delete[].
There is absolutely no reason I see why you need to resort to using char pointers. Stick with std::string.
Also, please use code tags when posting code. The code you have is unformatted and very hard to read.
Regards,
Paul McKenzie
Re: string comparing problem
Quote:
Originally Posted by
Philip Nicoletti
Summary of what you have ...
Code:
char * string_a = new char[loaded_values.size() + 1];
char * string_b = new char[loaded_values.size() + 1];
if(string_a==string_b)
You should not compare c-style strings using ==
You should use strcmp ...
Also:
You have global std::string variables named string_a and string_b , and you
also have local char* variables with the same name. This can be confusing.
****************************************************
Your suggestion that I instead use this:
if (strcmp(string_a,string_b) == 0)
worked fine. Plus I removed the 2 redundant globals. Thanks Philip!
Re: string comparing problem
Quote:
Originally Posted by
Paul McKenzie
In addition to what Philp stated, why are you using char pointers when you are using std::string everywhere else? Your program has a memory leak due to you using new[] without a proper call to delete[].
There is absolutely no reason I see why you need to resort to using char pointers. Stick with std::string.
Also, please use code tags when posting code. The code you have is unformatted and very hard to read.
Regards,
Paul McKenzie
Paul McKenzie[/QUOTE]
****************************************************
I have lots of code samples from an old PC with Borland 5.5 C++
hence the use of char (from an old code segment). Thanks for
reminding me to flush the memory, if I understand you, I should do this:
................................
writetext("starting.num", "2", "w");//in real life app this file should be set outside
loadfile("starting.num");
char * _startingnum = new char[loaded_values.size() + 1];
std::copy(loaded_values.begin(), loaded_values.end(), _startingnum);
delete[] loaded_values;
Right? Btw, I'm new to forums and don't know what a tag is,
unless you were saying I should attach the actual code file.
Re: string comparing problem
Quote:
Originally Posted by
RichardsPeter39yahoo
... I'm new to forums and don't know what a tag is,
unless you were saying I should attach the actual code file.
Check it out: Announcements (section "Information on posting");
BB code
Re: string comparing problem
Quote:
Originally Posted by
RichardsPeter39yahoo
****************************************************
I have lots of code samples from an old PC with Borland 5.5 C++
hence the use of char (from an old code segment). Thanks for
reminding me to flush the memory, if I understand you, I should do this:
................................
writetext("starting.num", "2", "w");//in real life app this file should be set outside
loadfile("starting.num");
char * _startingnum = new char[loaded_values.size() + 1];
std::copy(loaded_values.begin(), loaded_values.end(), _startingnum);
delete[] loaded_values;
Right?
No. The loaded_values is a std::string. It is "_startingnum" that needs to be deallocated.
But why do any of this when you could simply have done this:
Code:
std::string _startingnum = loaded_values;
Regards,
Paul McKenzie