how to use atoi!!!

This is a discussion on how to use atoi!!! within the C++ Programming forums, part of the General Programming Boards category; hello: did anyone know how to use atoi.........atoi is use to convert char to int right?????then how to use it.....i ...

  1. #1
    moon
    Guest

    how to use atoi!!!

    hello:
    did anyone know how to use atoi.........atoi is use to convert char to int right?????then how to use it.....i mean did anyone can show the example for me,please???thanks

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    main()
    {
    int val;
    long val2;
    char *string = "1234567";
    val = atoi(string); // converts to int
    val2 = atol(string); // converts to long
    return 0;
    }

    prototypes of atoi and atol.

    int atoi(const char *);
    long atol(const char *);
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  3. #3
    moon
    Guest
    hello

    u are using pointer right?????besides using pointer,do u know the easier way to use the atoi.....coz i wan to make my program shorter

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    atoi converts character strings only (whether you create strings with arrays or pointers, does not matter). It is seen from the prototype.

    Are you trying to convert a single character to integer (a digit) ? If this is the case you may try something like this.

    int a;
    char b = '6';
    a = b - '0';
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  5. #5
    moon
    Guest
    morning:
    hello,ozgulker......in my case.....i want to using atoi to make year......so got any idea???i dont want to use pointer coz i really cant understand how the pointer work....

    do u know how to make the date in DD/MM/YYYY format.....need to use % to search this'/' or what.......???

    then,i got last question here.......when i using characters array to write the file ,then when i read it,i cant read all...so how to solve it????
    now i doing a project about personnel detail..so i need to write quite a long sentences in the file,then the output of the file cannot display line by line coz some is long and some is short.....so do u know how to make it look nice and neet!!!!

    hope can hear from u soon.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-23-2009, 12:55 AM
  2. atoi() equivalent in C++
    By pandadc in forum C++ Programming
    Replies: 25
    Last Post: 09-27-2006, 01:28 PM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 02:59 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21