If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

string comparing problem

In my 96 line main.cpp code below, at lines 40-44 I can't get this loop to work:
if(string_a==string_b)
{
printf("string_a and string_b match\n");
return 0;
}
string_a and string_b are created from two identical files, named stringa.txt
and stringb.txt, both being 3 byte files containing: abc

If some kind person out there can take a few moments to create two such files
(create these two files in your own way without using my writetext function).

My frustration is that although these two 3 byte files are identical and although
I've verified they each both contain 'abc' by printing string_a and string_b ...
the fact remains that my loop at lines 40-44 fails to register that they match!

Perhaps my problem arises when I rename the string from loaded_values to string_a
and string_b, as per this code segment here:

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.

Re: string comparing problem

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

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.