Solved

How to convert CString to int?

Posted on 2007-03-30
10
960 Views
Last Modified: 2008-01-09
I cannot figure out why the following codes won't work to change a string to an interger. Please help!! The numbers are stored in the file seperated by single space. And the error code is: " error C2664: 'atoi' : cannot convert parameter 1 from 'CString' to 'const char *' "

void CGrindGameDlg::OnBnClickedFileOpen()
{
      // TODO: Add your control notification handler code here
      CStdioFile fBoundary;
      CString temp;

      CFileDialog FileDlg(TRUE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Points files (*.txt)|*.txt|All Files (*.*)|*.*|" ), NULL, 0);
      
      if( FileDlg.DoModal() == IDOK )
      {
            if( fBoundary.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
                  return;
            
            temp=FileDlg.GetFileName();

            // read the boundary points from the file and saved in vector sbound
            vector <i32> sboundx, sboundy;
            CString point;
            int n1,n2;
            int t;
            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi(temp);
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi(temp);
                  sboundy.push_back(t);
            }
}
0
Comment
Question by:applebibo
  • 5
  • 5
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18825968
Just use

 t=atoi((LPCTSTR)temp);

to get a 'char*' from the CString, then it will work.
0
 

Author Comment

by:applebibo
ID: 18826033
It did not work:
error C2664: 'atoi' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Thanks!
0
 
LVL 86

Expert Comment

by:jkr
ID: 18826077
Are you changing that at both locations?

            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi((LPCTSTR)temp);
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi((LPCTSTR)temp);
                  sboundy.push_back(t);
            }

If that still does not work, try

            while (fBoundary.ReadString(point))
            {
                  n1=point.Find(' ');
                  temp=point.Left(n1+1);
                  t=atoi(temp.GetBuffer(0));
                  temp.ReleaseBuffer();
                  sboundx.push_back(t);

                  n2=point.Find(' ',n1);
                  temp=point.Mid(n1+1,n2+1);
                  t=atoi(temp.GetBuffer(0));
                  temp.ReleaseBuffer();
                  sboundy.push_back(t);
            }
0
 

Author Comment

by:applebibo
ID: 18826101
Thanks! Yes, I did change both locations. And I just tried your code, but there are still errors:

error C2664: 'atoi' : cannot convert parameter 1 from 'wchar_t *' to 'const char *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

What's wrong with my Visual C++ 2005?
0
 
LVL 86

Expert Comment

by:jkr
ID: 18826189
>>error C2664: 'atoi' : cannot convert parameter 1 from 'wchar_t *'
>>to 'const char *'

Ah, you are using UNICODE - then you'll need to use '_wtoi()' instead
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 18826237
Or, since you are using TCHAR functions, '_ttoi()'...
0
 

Author Comment

by:applebibo
ID: 18826667
Thanks for getting this headache out of me!!!! You ARE genius!!!!
0
 

Author Comment

by:applebibo
ID: 18826844
Then do I need to change the function name when I compile it on other computers that are not using UNICODE?
0
 
LVL 86

Expert Comment

by:jkr
ID: 18826874
If you use '_ttoi()', you won't have to change anything.
0
 

Author Comment

by:applebibo
ID: 18826906
I get it. I can set it in the project properties.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 72
C++ Language error 28 145
Socket Programming (Unix) 8 98
C++ question 3 46
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now