Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Question about +=

Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
130
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
130
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.
if(m_nLevel > 99)
{
CString strName;
strName = szName;
if(m_nLevel == 100)
{
strName += " *Amara* 1";
}
if(m_nLevel == 101)
{
strName += " *Amara* 2";
}
if(m_nLevel == 102)
{
strName += " *Amara* 3";
}
if(m_nLevel == 103)
{
strName += " *Amara* 4";
}
if(m_nLevel == 104)
{
strName += " *Amara* 5";
}
if(m_nLevel == 105)
{
strName += " *Amara* 6";
}
if(m_nLevel == 106)
{
strName += " *Amara* 7";
}
if(m_nLevel == 107)
{
strName += " *Amara* 8";
}
if(m_nLevel == 108)
{
strName += " *Amara* 9";
}
if(m_nLevel == 109)
{
strName += " *Amara* 10";
}
if(m_nLevel == 110)
{
strName += " *Amara* 11";
}
if(m_nLevel == 111)
{
strName += " *Amara* 12";
}
if(m_nLevel == 112)
{
strName += " *Amara* 13";
}
if(m_nLevel == 113)
{
strName += " *Amara* 14";
}
if(m_nLevel == 114)
{
strName += " *Amara* 15";
}
if(m_nLevel == 115)
{
strName += " *Amara* 16";
}
if(m_nLevel == 116)
{
strName += " *Amara* 17";
}
if(m_nLevel == 117)
{
strName += " *Amara* 18";
}
if(m_nLevel == 118)
{
strName += " *Amara* 19";
}
if(m_nLevel == 119)
{
strName += " *Amara* 20";
}
if(m_nLevel == 120)
{
strName += " *Amara* 21";
}
if(m_nLevel == 121)
{
strName += " *Amara* 22";
}
if(m_nLevel == 122)
{
strName += " *Amara* 23";
}
if(m_nLevel == 123)
{
strName += " *Amara*24";
}
if(m_nLevel == 124)
{
strName += " *Amara* 25";
}
if(m_nLevel == 125)
{
strName += " *Amara* 26";
}
if(m_nLevel == 126)
{
strName += " *Amara* 27";
}
if(m_nLevel == 127)
{
strName += " *Amara* 28";
}
if(m_nLevel == 128)
{
strName += " *Amara* 29";
}
if(m_nLevel == 129)
{
strName += " *Amara* 30";
}
if(m_nLevel == 130)
{
strName += " *Amara* 31";
}
if(m_nLevel == 131)
{
strName += " *Amara* 32";
}
if(m_nLevel == 132)
{
strName += " *Amara* 33";
}
if(m_nLevel == 133)
{
strName += " *Amara* 34";
}
if(m_nLevel == 134)
{
strName += " *Amara* 35";
}
if(m_nLevel == 135)
{
strName += " *Amara* 36";
}
if(m_nLevel == 136)
{
strName += " *Amara* 37";
}
if(m_nLevel == 137)
{
strName += " *Amara* 38";
}
if(m_nLevel == 138)
{
strName += " *Amara* 39";
}
if(m_nLevel == 139)
{
strName += " *Amara* 40";
}
if(m_nLevel == 140)
{
strName += " *Eda* 1";
}
if(m_nLevel == 141)
{
strName += " *Eda* 2";
}
if(m_nLevel == 142)
{
strName += " *Eda* 3";
}
if(m_nLevel == 143)
{
strName += " *Eda* 4";
}
if(m_nLevel == 144)
{
strName += " *Eda* 5";
}
if(m_nLevel == 145)
{
strName += " *Eda* 6";
}
if(m_nLevel == 146)
{
strName += " *Eda* 7";
}
if(m_nLevel == 147)
{
strName += " *Eda* 8";
}
if(m_nLevel == 148)
{
strName += " *Eda* 9";
}
if(m_nLevel == 149)
{
strName += " *Eda* 10";
}
if(m_nLevel == 150)
{
strName += " *Eda* 11";
}
if(m_nLevel == 151)
{
strName += " *Eda* 12";
}
if(m_nLevel == 152)
{
strName += " *Eda* 13";
}
if(m_nLevel == 153)
{
strName += " *Eda* 14";
}
if(m_nLevel == 154)
{
strName += " *Eda* 15";
}
if(m_nLevel == 155)
{
strName += " *Eda* 16";
}
if(m_nLevel == 156)
{
strName += " *Eda* 17";
}
if(m_nLevel == 157)
{
strName += " *Eda* 18";
}
if(m_nLevel == 158)
{
strName += " *Eda* 19";
}
if(m_nLevel == 159)
{
strName += " *Eda* 20";
}
if(m_nLevel == 160)
{
strName += " *Eda* 21";
}
if(m_nLevel == 161)
{
strName += " *Eda* 22";
}
if(m_nLevel == 162)
{
strName += " *Eda* 23";
}
if(m_nLevel == 163)
{
strName += " *Eda*24";
}
if(m_nLevel == 164)
{
strName += " *Eda* 25";
}
if(m_nLevel == 165)
{
strName += " *Eda* 26";
}
if(m_nLevel == 166)
{
strName += " *Eda* 27";
}
if(m_nLevel == 167)
{
strName += " *Eda* 28";
}
if(m_nLevel == 168)
{
strName += " *Eda* 29";
}
if(m_nLevel == 169)
{
strName += " *Eda* 30";
}
if(m_nLevel == 170)
{
strName += " *Eda* 31";
}
if(m_nLevel == 171)
{
strName += " *Eda* 32";
}
if(m_nLevel == 172)
{
strName += " *Eda* 33";
}
if(m_nLevel == 173)
{
strName += " *Eda* 34";
}
if(m_nLevel == 174)
{
strName += " *Eda* 35";
}
if(m_nLevel == 175)
{
strName += " *Eda* 36";
}
if(m_nLevel == 176)
{
strName += " *Eda* 37";
}
if(m_nLevel == 177)
{
strName += " *Eda* 38";
}
if(m_nLevel == 178)
{
strName += " *Eda* 39";
}
if(m_nLevel == 179)
{
strName += " *Eda* 40";
}
if(m_nLevel == 180)
{
strName += " *Astica* 1";
}
if(m_nLevel == 181)
{
strName += " *Astica* 2";
}
if(m_nLevel == 182)
{
strName += " *Astica* 3";
}
if(m_nLevel == 183)
{
strName += " *Astica* 4";
}
if(m_nLevel == 184)
{
strName += " *Astica* 5";
}
if(m_nLevel == 185)
{
strName += " *Astica* 6";
}
if(m_nLevel == 186)
{
strName += " *Astica* 7";
}
if(m_nLevel == 187)
{
strName += " *Astica* 8";
}
if(m_nLevel == 188)
{
strName += " *Astica* 9";
}
if(m_nLevel == 189)
{
strName += " *Astica* 10";
}
if(m_nLevel == 190)
{
strName += " *Astica* 11";
}
if(m_nLevel == 191)
{
strName += " *Astica* 12";
}
if(m_nLevel == 192)
{
strName += " *Astica* 13";
}
if(m_nLevel == 193)
{
strName += " *Astica* 14";
}
if(m_nLevel == 194)
{
strName += " *Astica* 15";
}
if(m_nLevel == 195)
{
strName += " *Astica* 16";
}
if(m_nLevel == 196)
{
strName += " *Astica* 17";
}
if(m_nLevel == 197)
{
strName += " *Astica* 18";
}
if(m_nLevel == 198)
{
strName += " *Astica* 19";
}
if(m_nLevel == 199)
{
strName += " *Astica* 20";
}
if(m_nLevel == 200)
{
strName += " *Astica* 21";
}
strcpy(szName, (LPCTSTR)strName);
}
 

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
130
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
if (m_nLevel > 99 && m_nLevel < 140) {
int i = m_nLevel; // Initialization expression
string k = std::to_string(m_nLevel - 99);
string l = "Amara" + k;
int j = m_nLevel + 1;
do
{
// loop body
strName += l.c_str();
//cout << "Hello World\n";

// update expression
i++;

} //while (i < 140); // test expression
while (i < j);

}
 
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();
}
 
Back
Top