Hello everyone, I'm new to the forum and new to C++, or any programming for that matter. I've been trying to create suite of 4 functions one of which takes a string and converts it into an integer. I'm aware of the atoi function but we are not supposed to use it, we need to creat our own. I've tried to read the atoi code to get a general idea of how it works, but I can't make sense of it, nor can I make much sense of the other similiar functions that pulled up when I did a site search (I'm really having a hard time grasping this language). I don't have to worry about validating the string to make sure it's a valid integer, I have written a seperate function for that as part of this function suite. Here is the funtion and the test driver for it:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int string2Int(char string[]);
main()
{      char test_string[81];
       int status;
       printf("\nEnter the string to convert: ");
       scanf("%s",test_string);
       printf("\nEchoing input string: %s",test_string);
       status=string2Int(test_string);
       printf("\nconverted integer = %d",status);
       printf("\n");
       system("pause");
}
int string2Int(char *s)
#include<math.h>
{
    int result=0, i=0, length, sign=0;
    length = strlen(s);
    if(s[0]=='-')
       {
        i++;
        sign=1;
        }
    for(;i<length;i++)
       {
        result=(("%d",s[i])-48);
        result=result*pow(10,i);
       }
    if(sign)
       {
        result=result*-1;
       }
    return(result);
}

lets say I have string 234. What I'm trying to do is take the asking code of character 2 (50) subtract 48 = 2 then multiply 2 by 10 to the power of the position in lays in the string in this example position 2 so
(50-48)*10^2=200
(51-48)*10^1=30
(52-48)*10^0=4
200+30+4 would = integer 234

I'm open to other ideas of how to do this too, the above was just how I thought I might be able to do it, but I just can't figure it out and I've been working on it for 2 days now....

You can check the code library for it, i think i saw it there

You can check the code library for it, i think i saw it there

Code library? Are you talking about I can find the atoi code? I'm not wanting that code I need to create a code that performs the similiar task though. I'm not sure what/where the code library is I'm new to the site, please excuse my arrogance. I'll have a look around.

On the other side I got it to work... kind of. My function will convert a string that is < 3 characters long or > 3characters long but < 10 characters long. I have no idea why this is here is the revised function and test driver.

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int string2Int(char *s);
main()
{      char test_string[81];
       int status;
       printf("\nEnter the string to convert: ");
       scanf("%s",test_string);
       printf("\nEchoing input string: %s",test_string);
       status=string2Int(test_string);
       printf("\nconverted integer = %d",status);
       printf("\nlenght of string=%d", strlen(test_string));
       printf("\n");
       system("pause");
}
int string2Int(char *s)
#include<math.h>
{
    int result=0, i=0, length=0, sign=0,x=(strlen(s)-1);
    length = strlen(s);
    if(s[0]=='-')
       {
        i++;
        sign=1;
        }
    for(i;i<length;i++)
       {
        result=result+(("%d",s[i]-48)*pow(10,x));
        x=x-1;
       }
    if(sign)
       {
        result=result*-1;
       }
    return(result);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.