Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / strings in C++... so far nothing works or app crashes

Author
Message
mindsclay
11
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 5th Oct 2015 11:58 Edited at: 5th Oct 2015 12:21
I was attempting to convert my basic code into C++ and then continue from there. All seems well except I suspected issues with the strings.

So, I attempted a small and very simple program to concatenate two strings then print the result. I can print a char*. I can print a std:string. I can even print agk::Str(a) (a being whatever).
But as soon as I try to concatenate, the app/program will crash. No compile errors or any other error message except "the application has stopped working".

How did we go from the simple string manipulation of BASIC to the convoluted string nightmare in C++?? Python is more straight forward than C. None of the examples I have found for manipulating strings in C++ work. Even when I include the class such as <string>, <sstream>, <iostream> and any of the others described in the examples. (I didn't get a crash when I used cout but also did not get a display.)

For example, here is one method that is suppossed to work for concatenating character strings. I have tried many different methods including strcat(), but all cause a crash.


The compiler will not let this happen:


This will also cause a crash:


Yet, this does not:


Merely trying to add strings together like in the straight forward BASIC or Python. I am trying to convert char to string, manipulate, then convert back string to char.

Does anyone else get crashes?
home.wavecable.com/~mindsclay
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 5th Oct 2015 13:08
C-Strings can be a little tricky to work with. It's important to realize that a C-String is really just an array with characters and the last element of the array contains a null character to represent the end of the string. Luckily, you shouldn't have to deal with all that instead just use the string library that is provided with Visual Studio (actually it's apart of the STL).

Something like this will then work:






Sean
mindsclay
11
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 6th Oct 2015 06:58
Thanks again for that help. I do understand that strings are arrays. But in Python arrays are lists.

Here is another anomaly. Again converting a string to integer, but this time trying to concatenate in SetTextString.

In this one, the agk::Str() part has the error:


But when I try to fix the above error, the quoted string now has the error.




Why would there be different errors? Just trying to understand the idiosyncrasies...
home.wavecable.com/~mindsclay
mindsclay
11
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 6th Oct 2015 07:14
Wait... The compiler isn't liking this bit: MainLoop.RRscroll.invItemSelected. So this is probably because there is an error somewhere else...
home.wavecable.com/~mindsclay
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 6th Oct 2015 11:00
Quote: "In this one, the agk::Str() part has the error:"


"SetTextString" expects a char but by concatenating two "strings" like this "invItemSelected= " + agk::Str(MainLoop.RRscroll.invItemSelected) you are really trying to concatenate two char * together. Which, we know that the + operator cannot be used to concatenate char *. The reason that the "string" class can use the operator+ to concatenate is because the class has an operator+ overload to support concatenation using +.

Quote: "But when I try to fix the above error, the quoted string now has the error."



Now this is almost correct, "agk::SetTextString(3, "invItemSelected= " + itemSelected)." The issue is that using " "invItemSelected= " + itemSelected" is returning a string, but SetTextString takes a C-String. It can be fixed by using parenthesis around the resulting string to invoke the "c_str()" method.
This should work:




Also, agk::Str() returns a char * so you need to use "delete []" to free the memory from it once you are done using it.


Sean
mindsclay
11
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 9th Oct 2015 03:04 Edited at: 9th Oct 2015 03:06
Thanks, Sean. I was one step away from that, but got frustrated.

I do not know yet if that works until I finish converting all the code. But, at least there are no errors.
home.wavecable.com/~mindsclay

Login to post a reply

Server time is: 2024-04-19 08:01:27
Your offset time is: 2024-04-19 08:01:27