char int string ,the different? and string 2 double convent problem.

Discussion in 'C++' started by key9, Nov 29, 2006.

  1. key9

    key9 Guest

    Hi all

    on reading code I found these function

    int foo(int char);
    char foo(char ch_);

    string foo(string ch);


    I know the different between char and the string

    char[]: 'this is char\0'
    string: 'this is string'
    What about int?


    Also
    How to corvent string to double, for example

    string: 12.0317 to double

    an "safe" conversion should first confirm the '12.0317' is double
    using regexp? that's too big.
    or buy scan the string using isdigital()?

    and what about 002.0317?


    thank you
    key9
     
    key9, Nov 29, 2006
    #1
    1. Advertising

  2. key9

    mlimber Guest

    key9 wrote:
    > on reading code I found these function
    >
    > int foo(int char);
    > char foo(char ch_);
    > string foo(string ch);


    Assuming you mean std::string, that should *probably* be:

    string foo( const string& ch);

    >
    >
    > I know the different between char and the string
    >
    > char[]: 'this is char\0'


    No, that's a character array. A char like the parameter to foo() is a
    single character.

    > string: 'this is string'
    > What about int?


    An int is a number, a primitive data type in C++. What more do you want
    to know?

    Your real question is probably related to function overloading.

    foo( 1 ); // Calls foo(int)
    foo( '1' ); // Calls foo(char)
    foo( "1" ); // Calls foo(string)

    > Also
    > How to corvent string to double, for example
    >
    > string: 12.0317 to double
    >
    > an "safe" conversion should first confirm the '12.0317' is double
    > using regexp? that's too big.
    > or buy scan the string using isdigital()?
    >
    > and what about 002.0317?


    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.2

    Cheers! --M
     
    mlimber, Nov 29, 2006
    #2
    1. Advertising

  3. * key9:
    > Hi all


    Hi hi!


    > on reading code I found these function
    >
    > int foo(int char);
    > char foo(char ch_);
    >
    > string foo(string ch);
    >
    >
    > I know the different between char and the string
    >
    > char[]: 'this is char\0'
    > string: 'this is string'


    Well, almost. If the idea is that a char /array/ is zero-terminated,
    then you have it almost right. It's not necessarily zero-terminated (in
    fact there are zillions of string formats), but the most common format,
    and the one you get for a literal string constant such as "uhuh", is
    zero-terminated.

    Example (off the cuff):

    #include <cstdio> // strlen
    #include <iostream> // cout
    #include <ostream> // <<, endl
    #include <string> // string

    int main()
    {
    using namespace std;

    char const s[] = "uhuh";
    string const t = s; // Copy, in std::string format.

    cout << sizeof( s ) << endl; // 5, not 4.
    cout << strlen( s ) << endl // 4.
    cout << t.length() << endl; // 4.
    }


    > What about int?


    It's an integer type. You can do integer arithmetic.



    > Also
    > How to corvent string to double, for example
    >
    > string: 12.0317 to double


    boost::lexical_cast

    Install the Boost library.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    406
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,657
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,070
    Ian Collins
    May 9, 2006
  5. Shriramana Sharma
    Replies:
    8
    Views:
    277
    Gerhard Fiedler
    Jun 18, 2013
Loading...

Share This Page