Solved

How to convert CString to int?

Posted on 2007-03-30
10
994 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
Technology Partners: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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