Question about +=

Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
This is in Visual Studio 2017 C++
This is my formula.
//m_nLevel = anywhere from 100 to 139 based on the level of the character being played.
int a = m_nLevel;
int b = a - 99;
strName += " *Amara*" + b;//The output I am looking for is " *Amara*1" through " *Amara*39" but, the only result I get is subtraction of the letters and not a output of what I want. Any help would be appreciated. Thank you.
 
Joined
Aug 14, 2009
Messages
1,097
Reaction score
134
Here's a stupid question: is m_nLevel even an integer? Is it for some reason a char?
I mean, in theory it shouldn't even matter seeing as you're assigning it into a new int...
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
if you are using std::string, you should use std::to_string, to cast an int to a string.
std::string strName = "";
...
strName += " *Amara*" + std::to_string(b);

if you don't convert the number into a string when you concatenate with the operator+, it will cast the int to a zero terminating string, in your case 1 will be 0x01\0(start of the heading), then it concatenates with your string.

 
Last edited:
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
strName += " *Amara*" + b; //" *Amara*" is (const char*)[9] and b is int, strName is CString
I have tried your method GHOST107 but, it then gives me an opperand error with +=.
TimeBomb What I want is *Amara* 1 - *Amara* 39 on the end based on the value of b. What I am currently getting is results like Amara*, mara*, ara* and so on. So it is subtracting the letters instead of simply placing a number value on the end of it.
If anything else is required please let me know. I will gladly comply. Thank you.



This is my ultimate goal but, to shorten it up quite a bit.
Thanks.
 

nck

Newbie Spellweaver
Joined
Nov 6, 2018
Messages
6
Reaction score
7
Perhaps a solution like this?
Code:
LPCTSTR szName = _T("Hello"); 

CString strName = szName;

int m_nLevel = 120; // 100 ~ 139
int b = m_nLevel - 99;

strName.Format(_T("%s *Amara*%d"), szName, b);
szName = (LPCTSTR)strName;

std::wcout << szName << std::endl; // Hello *Amara*21
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
You could try to overload the operator += and +(if operator + is not defined in CString):

Convert to string:
Code:
CString to_CString(int nValue)
{
    // Convert int to zero terminated string for CString
}

CString to_CString(short nValue)
{
    // Convert short to zero terminated string for CString
}

CString to_CString(long long nValue)
{
    // Convert long long to zero terminated string for CString
}

CString to_CString(float nValue)
{
    // Convert float to zero terminated string for CString
}

CString to_CString(double nValue)
{
    // Convert double to zero terminated string for CString
}

// other functions to be converted

Operator +=
Code:
void operator+=(CString& left, const char* right)
{
   // handle operator +=
}

void operator+=(CString& left, CString& right)
{
   // handle operator +=
}

void operator+=(CString& left, CString&& right)
{
   operator+=(left, right);
}

Operator +
Code:
CString operator+(const char* left, CString& right)
{
   // handle operator+
}


CString operator+(CString& left, CString& right)
{
   // handle operator+
}

CString operator+(CString& left, CString&& right)
{
   return operator+(left, right);
}

CString operator+(CString&& left, CString& right)
{
   return operator+(left, right);
}

CString operator+(CString&& left, CString&& right)
{
   return operator+(left, right);
}
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
Isn't there a loop function that might do the same thing? Maybe I am overthinking this somehow.



By golly I think I got it now. LOL
 
Joined
Aug 14, 2009
Messages
1,097
Reaction score
134
Isn't there a loop function that might do the same thing? Maybe I am overthinking this somehow.



By golly I think I got it now. LOL

I feel like I'm missing something. Why are you using a loop?

After some indenting I came up with this:

Code:
if (m_nLevel > 99 && m_nLevel < 140) {
	int level = m_nLevel;
	string k = std::to_string(m_nLevel - 99);
	string l = "Amara" + k;
	int displayLevel = m_nLevel + 1;

	do {
		strName += l.c_str();
		level++;
	} while (level < displayLevel);
}

But, there's a lot of unnecessary stuff going on there. Untested, but I feel this is probably equivalent to what you were doing...? And even this is going a little all-in on the individual variables.
Code:
if (m_nLevel > 99 && m_nLevel < 140) {
	std::string k = std::to_string(m_nLevel - 99);
	strName += std::string("Amara " + k).c_str();
}