Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert CString to int or float

Convert CString to int or float

Scheduled Pinned Locked Moved C / C++ / MFC
c++
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Trupti Mehta

    In Evc++4, I want to convert CString to int/float. I use the following code :

    // CONVERT STRING TO INT
    CString csValue;
    GetDlgItem(IDC\_OpNo\_EDIT)->GetWindowText( csValue );
    m\_opNo = atoi((char\*)(LPCTSTR)csValue);
    

    The above code works, but only with the first digit, so If I enter 34, I only get 3 in m_opNo int. same happens with float atof. Can anyone tell what changes ae required to get full digit from CString to int. Is I enter 34 I want 34 as int.

    Thanks Terry

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #5

    Trupti Mehta wrote:

    m_opNo = atoi((char*)(LPCTSTR)csValue);

    What's with all the casts?

    "Love people and use things, not love things and use people." - Unknown

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    1 Reply Last reply
    0
    • C CPallini

      killabyte wrote:

      the wise Pallini

      like Alfonso of my sign. :laugh: Thank you. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      K Offline
      K Offline
      killabyte
      wrote on last edited by
      #6

      Yeah i really liked that quote in your sig :-D

      1 Reply Last reply
      0
      • C CPallini

        killabyte wrote:

        the wise Pallini

        like Alfonso of my sign. :laugh: Thank you. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        SandipG
        wrote on last edited by
        #7

        I like second more :D :D

        Regards, Sandip.

        1 Reply Last reply
        0
        • T Trupti Mehta

          In Evc++4, I want to convert CString to int/float. I use the following code :

          // CONVERT STRING TO INT
          CString csValue;
          GetDlgItem(IDC\_OpNo\_EDIT)->GetWindowText( csValue );
          m\_opNo = atoi((char\*)(LPCTSTR)csValue);
          

          The above code works, but only with the first digit, so If I enter 34, I only get 3 in m_opNo int. same happens with float atof. Can anyone tell what changes ae required to get full digit from CString to int. Is I enter 34 I want 34 as int.

          Thanks Terry

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #8

          As DavidCrow asked....what's with the casts? I always recommend using NO casts unless absolutely necessary. If something doesn't compile without the cast, look CLOSELY AT WHY before casting away the problem. In this case, you casted away your compile-time problem and turned it into a run-time problem :) Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • C CPallini

            int i = _ttoi(csValue);

            double f = _tstof(csValue);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            T Offline
            T Offline
            Trupti Mehta
            wrote on last edited by
            #9

            Thanks CPallini & everybody else. This solved my problem for int's but not for float as _ttof or _tstof id not defined in tchar also. I am using eVc++4.0 with WinCE 5. I can't find the functions in eVC help or tchar.h file also & it throws compilation error : error C2065: '_tstof' : undeclared identifier Any other alternative to do the above task would be great.

            Thanks Terry

            C 1 Reply Last reply
            0
            • T Trupti Mehta

              Thanks CPallini & everybody else. This solved my problem for int's but not for float as _ttof or _tstof id not defined in tchar also. I am using eVc++4.0 with WinCE 5. I can't find the functions in eVC help or tchar.h file also & it throws compilation error : error C2065: '_tstof' : undeclared identifier Any other alternative to do the above task would be great.

              Thanks Terry

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #10

              Well you can rool your own version:

              #ifdef _UNICODE
              #define _tstof wtof
              #else
              #define _tstof atof
              #endif

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              T 1 Reply Last reply
              0
              • C CPallini

                Well you can rool your own version:

                #ifdef _UNICODE
                #define _tstof wtof
                #else
                #define _tstof atof
                #endif

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                T Offline
                T Offline
                Trupti Mehta
                wrote on last edited by
                #11

                Thanks CPallini for your quick response. I entered your code on top of my file (after #includes) & used _tstof(csValue), but it gives me the same error for wtof. I also can't find wtof or _wtof in Help of Evc++4. wtoi is available but not wtof. TChar.h is included. error C2065: 'wtof' : undeclared identifier Any further help, please.

                Thanks Terry

                C 1 Reply Last reply
                0
                • T Trupti Mehta

                  Thanks CPallini for your quick response. I entered your code on top of my file (after #includes) & used _tstof(csValue), but it gives me the same error for wtof. I also can't find wtof or _wtof in Help of Evc++4. wtoi is available but not wtof. TChar.h is included. error C2065: 'wtof' : undeclared identifier Any further help, please.

                  Thanks Terry

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #12

                  If ypur system really doesn't provide wtof, you may consider to convert your wide character string into standard (i.e. ANSI) one, via WideCharToMultiByte), and then use atof. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  T 1 Reply Last reply
                  0
                  • C CPallini

                    If ypur system really doesn't provide wtof, you may consider to convert your wide character string into standard (i.e. ANSI) one, via WideCharToMultiByte), and then use atof. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    T Offline
                    T Offline
                    Trupti Mehta
                    wrote on last edited by
                    #13

                    Hello, Thanks. I tried the following way :

                    CString csValue;
                    GetDlgItem(IDC\_PRICE\_EDIT)->GetWindowText(csValue);
                    

                    .....
                    char* dest;
                    WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, 10, NULL, NULL);
                    float flt = (float)atof(dest);
                    CString d(_T("Converted Float:"));
                    d.Format(_T("%s %.2f"), d, flt);
                    AfxMessageBox(d);

                    Using WideCharToMultiByte method, I put all values accordingly & converted to float using atof. I get the converted value. BUT as soon as the Converted .. msg box is displayed I also get "Assertion Failed!" :File thrdcore.cpp, Line 867 Abort/Ret/Ig. Retry or ignore doesn't respond so I have to select Abort. It points to if (m_nDisablePumpCount != 0) { TRACE0("Error: CWinThread::PumpMessage called when not permitted.\n"); --------> ASSERT(FALSE); } of PumpMessage(). Where am aI going wrong can you point out? Do I need to change anything in Wide...(). AS dest is not initialized, I get warning message for that also. I have also noted that in eVC++4 I get exceptions at Assert many times, any special reason?

                    Thanks Terry

                    C 1 Reply Last reply
                    0
                    • T Trupti Mehta

                      Hello, Thanks. I tried the following way :

                      CString csValue;
                      GetDlgItem(IDC\_PRICE\_EDIT)->GetWindowText(csValue);
                      

                      .....
                      char* dest;
                      WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, 10, NULL, NULL);
                      float flt = (float)atof(dest);
                      CString d(_T("Converted Float:"));
                      d.Format(_T("%s %.2f"), d, flt);
                      AfxMessageBox(d);

                      Using WideCharToMultiByte method, I put all values accordingly & converted to float using atof. I get the converted value. BUT as soon as the Converted .. msg box is displayed I also get "Assertion Failed!" :File thrdcore.cpp, Line 867 Abort/Ret/Ig. Retry or ignore doesn't respond so I have to select Abort. It points to if (m_nDisablePumpCount != 0) { TRACE0("Error: CWinThread::PumpMessage called when not permitted.\n"); --------> ASSERT(FALSE); } of PumpMessage(). Where am aI going wrong can you point out? Do I need to change anything in Wide...(). AS dest is not initialized, I get warning message for that also. I have also noted that in eVC++4 I get exceptions at Assert many times, any special reason?

                      Thanks Terry

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #14

                      Trupti Mehta wrote:

                      Where am aI going wrong

                      You didn't allocate memory for the dest buffer. The buffer should large enough to get the converted string. Hence you may write

                      int iLen = csValue.GetLenght();
                      char * dest = new char[iLen+1];
                      WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, iLen, NULL, NULL);
                      // Perform conversion to float
                      delete [] dest;

                      or, using a reasonably large array:

                      char dest[MAX_PATH+1];
                      WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, MAX_PATH, NULL, NULL);

                      :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      T 1 Reply Last reply
                      0
                      • C CPallini

                        Trupti Mehta wrote:

                        Where am aI going wrong

                        You didn't allocate memory for the dest buffer. The buffer should large enough to get the converted string. Hence you may write

                        int iLen = csValue.GetLenght();
                        char * dest = new char[iLen+1];
                        WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, iLen, NULL, NULL);
                        // Perform conversion to float
                        delete [] dest;

                        or, using a reasonably large array:

                        char dest[MAX_PATH+1];
                        WideCharToMultiByte(CP_ACP, 0, csValue, -1, dest, MAX_PATH, NULL, NULL);

                        :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        T Offline
                        T Offline
                        Trupti Mehta
                        wrote on last edited by
                        #15

                        Thanks CPallini, its working perfectly as expected. Got rid from all type casts. Thanks a lot to all of you trying to solve my problem.

                        Thanks Terry

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups