Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well I know it sounds funny. But because I'm working on a program that was previously
written and I have to make a simple calculation.

There is function that has a return type of CString and its is generally used to
write the value in a report. But now I have to make a simple subtraction from that CString value. Is that possible ?
Posted

You will have to convert it to a number first!
If you know that the string is always a number, and valid, then just use atoi[^]
If you aren't sure that it always will be, then use sscanf:
C#
int value;
int convCount;
convCount = sscanf(strInt, "%d", &value );
You can then check convCount: if it is one, then it worked.
 
Share this answer
 
Comments
Sumal.V 6-Apr-12 4:56am    
Oh well I tried the atoi. But it doesn't work .
int i;
i = atoi (myValue->GetText());

Here myValue is a variable and the GetText()function returns its value.

But the error says: No suitable conversion exists between CString to const char
OriginalGriff 6-Apr-12 5:12am    
It may be unicode: try _wtoi() or _ttoi()
Sumal.V 6-Apr-12 5:25am    
Hey Thanks. That works. But I have problems displaying the string using the string.append . But rather than just coying codes and juggling with errors, I will some some reading on strings first.
OriginalGriff 6-Apr-12 5:35am    
Good idea! :)
Monjurul Habib 6-Apr-12 5:13am    
5!
As already suggested you may convert the CString variable into a numeric one, before performing the subtraction (and then convert the result back to a CString in order to display it). However you should be also able to track down where the CString originates and modify the program (that is you should store numeric values in numeric variables and convert them to strings only when you need to represent them).
 
Share this answer
 
Comments
OriginalGriff 6-Apr-12 5:12am    
Yep! Gets my vote every time!
CPallini 6-Apr-12 8:27am    
Thank you.
BTW you've already gotten mine.
Monjurul Habib 6-Apr-12 5:13am    
5!
An alternative to sscanf is to use std::stringstream. However you need to convert CString to std::string and vice versa

Here a snippet from Codeguru how to do just that, and then I'll tell you how to use StringStream

CString cs ("Hello");

// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);

And now comes the stringstream part. Here's how I would do it.
//here's the std::string
std::string ss("10");

//use the string as an input stream
istringstream astream(ss,ios::in);

int val;

//extract the value from the input stream
astream>>val;
val -= 1;

//now use the string as an output stream
ostringstream os(ios::out);

//extract the value
os<<val;
//create a string out of it
string ass = os.str();
//convert your std::string to a CString
CString final(ass.c_str());
 
Share this answer
 
Comments
Sumal.V 6-Apr-12 9:28am    
Oh That looks a bit confusing. Will definitely go through the program again.
pmissier 6-Apr-12 21:29pm    
not really! using stringstreams is actually much simpler and less error prone! as a first step just compile my code and see whether it runs!! remember to include the following headers :

string
sstream
atlstr.h

and also add "using namespace std;"
However you should be also able to track down where the CString originates and modify the program (that is you should store numeric values in numeric variables and convert them to strings only when you need to represent them).
 
Share this answer
 
Comments
Sumal.V 6-Apr-12 6:41am    
Yeah I tried converting it back but doesn't display at all.
My function:
int i;
char buffer[20];
CString str;

i= _ttoi(myVal->GetText()); //convert string to int
itoa (i,buffer, 10); //convert int back to string
str.AppendFormat(_T("Value is : %s\n"), buffer);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900