CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Convert String Value to an Int

    Hi All,

    In C++ how do I convert the string value position to a integer. The compiler keeps on giving me an error.

    string numFamilyMembers[100];

    if(atoi(numFamilyMembers[0]) > 5){
    special=0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert String Value to an Int

    Quote Originally Posted by stephenalistoun View Post
    Hi All,

    In C++ how do I convert the string value position to a integer. The compiler keeps on giving me an error.

    string numFamilyMembers[100];

    if(atoi(numFamilyMembers[0]) > 5){
    special=0;
    }
    What is the parameter type that atoi( ) expects? What does the std::string::c_str() function return?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Re: Convert String Value to an Int

    Thanks Paul, for replying to my forum

    The atoi function expects a 5

    The compiler returns the following error:
    main.cpp:46: error: cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)

    I'm not sure what the std::string::c_str() returns.

    Regards,

    Stephen

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Convert String Value to an Int

    look it up: www.cplusplus.com. atoi expects a const char *, you are giving it a std::string.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Convert String Value to an Int

    Quote Originally Posted by stephenalistoun View Post
    Thanks Paul, for replying to my forum

    The atoi function expects a 5
    No, I didn't ask what value you're sending the function. I asked the parameter type. The type that atoi() expects is a const char *, therefore your parameter must have that type -- const char *. A 5 is not a constant char pointer.

    The c_str() function returns a const char*, and that is what atoi( ) is expecting.

    http://www.cplusplus.com/reference/c.../cstdlib/atoi/
    http://www.cplusplus.com/reference/string/string/c_str/

    I replied in the way I did in my original post because whatever the function is that you're calling, you must know the parameter type for each parameter. Otherwise you're not understanding how to call any function when certain parameter types are specified. Always understand what the function you're calling expects as arguments -- don't guess, as everything is (or should be) documented.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 22nd, 2011 at 11:25 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured