Atoi()

This is a discussion on Atoi() within the C++ Programming forums, part of the General Programming Boards category; Hello. I am trying to implement atoi(). Below is my code. It compiles correctly, but output is not correct. can ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    Atoi()

    Hello.

    I am trying to implement atoi(). Below is my code. It compiles correctly, but output is not correct. can anyone correct what wrong i am doing in logic here.
    Here is my code.

    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    using namespace std;
    
    int myAtoi(const char*);
    
    int main(){
        char abc[] = {"234"};
        int c = myAtoi(abc);
        cout<<c;
        system("PAUSE");
        }
        
    int myAtoi(const char* str){
        if(str == NULL){
               cout<<"String is empty";
               return 0;
               }
        int sum = 0, j, len;
        len = strlen(str);
        j = len-1;
        while(j>=0){
                    sum = sum + (((int)str[j]) * (pow(10.0,j)));
                    j--;
                    }
        return sum;
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Your problem is here:
    (int)str[j] <- you do not simply typecast a character to an integer, as you will just get the ascii code.

    I don't know if I'm allowed to post outside links but: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    That will tell you the ascii codes for characters, as you can see the ascii codes for numbers span from 0x30 to 0x39 (i find this easier to remember than 48 to 57), so to convert a number e.g. '2' (0x32) to integer, you subtract 0x30.

    You should not simply subtract 0x30 and call it a day though, as the character might not be a number, so you should stop when you reach a character that is not within 30-39 (i.e. not a number), for this reason I do not recommend iterating backwards through the string but forwards (i.e. j = 0 while j < len)
    Last edited by Samzor; 07-19-2010 at 08:43 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Thanks.

    I modified code as below, but still answer is not correct.
    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    using namespace std;
    
    int myAtoi(const char*);
    
    int main(){
        char abc[] = {"234"};
        cout<<strlen(abc)<<endl;
        int c = myAtoi(abc);
        cout<<c;
        system("PAUSE");
        }
        
    int myAtoi(const char* str){
        if(str == NULL){
               cout<<"String is empty";
               return 0;
               }
        int sum = 0, j, len, k = 0;
        len = strlen(str);
        j = len-1;
        while(j>=0){
                    sum = sum + ((str[j] - 30) * (pow(10.0,j)));
                    j--;
                    }
        return sum;
    }

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    I meant the hexademical 0x30 to 0x39, sorry (subtract 0x30 or 48).

    Also your powers are incorrect, as you get further down the string, the exponent should decrease (the number to the farmost right is the least significant so should get a power of 0, use len-j-1).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    It is better to simply subtract '0', which is more informative than 0x30.
    And to check the range, check if it's between '0' and '9'. Again, more informative.
    Not all platforms use ASCII.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complete function definition i.e. atoi
    By c_lady in forum Linux Programming
    Replies: 11
    Last Post: 03-31-2010, 12:28 PM
  2. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 03:53 PM
  4. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM

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