?
Solved

How to convert CString to int?

Posted on 2007-03-30
10
Medium Priority
?
1,020 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 86

Accepted Solution

by:
jkr earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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