Solved

How to convert CString to int?

Posted on 2007-03-30
10
1,005 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
Industry Leaders: 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

695 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