converting string to integer program problems

Pages: 12
I'm trying to make a suite of 4 functions one of which will take a string containing a valid integer number and converts that number into an integer. The professor mentioned something about haveing to subtract asking codes. I've played around with it and tried multiple variations but I'm really struggling with it. One of the four functions is to validate if the string is a valid integer, so there is no need to worry about validating the string 1st. Like I said earlier I was tring to play with subtracting asking codes to convert it into a string, as we were told too. Here is the last thing I tried. I'm really having difficulty figuring this programming stuff out so please take it easy on me. Here is the code for the function and the driver I'm using to test my function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.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 = ",status);
       printf("\n");
       system("pause");
}

int string2Int(char *s)
{
    int conint, i, length;
    length = strlen(s);
    for(i=0;i<length;i++)
       {
        conint=(("%d", 's[i]')-("%d"-1,'s[i]'));
       }
    return(conint);
}


Thanks in advance

~~The new newb
Line 22 doesn't make much sense
basically what I was trying to figure out (and I'm sure thats where my problem lays) was converted integer (conint) = the asking code of character in the array - ((the asking code of the character in the array) - 1)

ex:
asking code for integer 1 is 49 so to convert character 1 in a string to an integer 1 I would subtract 49 - (49-1) and that would give me integer 1. At least thats how I understood it during lecture. I'm just not sure how to code it. I tried a few different things and what is shown was my last far fetched attempt.
Assume x as the character:
x-(x-1) = 1 ∀x
What you want is x-48 where 48 is the value of '0' ( ASCII zero ) but that will work for the first digit.
If you want to continue, you should multiply that by 10 and insert the second one

This may help: http://www.cplusplus.com/articles/numb_to_text/#bad
Okay, you lost me. I'm really having a hard time grasping c language. I read through the link and can't really make sense of how to apply is towards what I'm doing.

What is that symbol between the 1 and x?
It means "for all" as in "equals one for all x".
http://en.wikipedia.org/wiki/Universal_quantification

The very last [code] block in the link Bazzy gave you shows how to do exactly the thing you are trying to do. Take a look at lines 13 through 17 in particular.

Good luck!
Ah I got it line 11 I had forgot to put in my %d inorder to print the integer status. and it apears my strange conversion code works on line 22. Thanks all for your input
Well after some sleep and some more testing it appears my strange code on line 22 isn't working.
Well I'm still plucking away at my problem, I've gotten to a point where I can successfully convert a string to and integer as long as the string is not 3 characters long or 9 or more characters long.

if the string is 3 or 9 characters long then I get a converted integer that 1 less than the string
(IE: if string =345 converte integer will =344)

I've been working on this for two days now I would greatly appreciate is if some one could really break what I'm doing wrong down for me, and please really put it in BEGINNERS terms becasue I'm struggling big time here. I've looked at the code Bazzy linked to but I really can't understand what is going on in it. I tried to rewrite it in the way I thought it read and I caused all sorts of problems with function.

here is the updated code and test driver I currently have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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;
        x=0;
        }
    for(i;i<length;i++)
       {
        result=result+(("%d",s[i]-48)*pow(10,x));
        x=x-1;
       }
    if(sign)
       {
        result=result*-1;
       }
    return(result);
}
Remove line 18
On line 30 you still have a nonsense "%d"
Okay done, same problem but simplified code thank you Bazzy
the reason I had %d was I though I needed it to read the decimal number at s[i]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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)
{
    int result=0, i=0, length=0, sign=0,x=(strlen(s)-1);
    length = strlen(s);
    if(s[0]=='-')
       {
        i++;
        sign=1;
        x=0;
        }
    for(i;i<length;i++)
       {
        result=result+((s[i]-48)*pow(10,x));
        x=x-1;
       }
    if(sign)
       {
        result=result*-1;
       }
    return(result);
}
If it compiles fine you can focus on the math part.
Line 29 is wrong. It will give a higher value to digits on the right, you want the opposite way.
Multiplying result by 10 and adding the current digit is the right way of doing this, This way you can avoid the variable x and the pow call.
Yea it's always complied fine, I'll work on line 29
Bazzy thank you so much, I've been pulling my hair out. The reason I was trying to use the pow call was because I had a blip in my notes on using that method to convert the string to integers. Anyways I have one more question why do I get a strange number when I enter a character string of 11 or more characters.

ex:
I enter character string of 12345678901
The converted integer comes out -539222987

Is it because I'm using data type int?

Here is my new code
1
2
3
4
5
    
for(i;i<length;i++)
       {
        result=result*10+(s[i]-48);
       }


I feel so dumb right now, haha. Talk about over thinking the mathmatics. Thanks again Bazzy.
One more question, during my research I read about instead of subtracting 48 I could subtract by hard code '0'. What does hard code '0' mean?
'0' and 48 are the same thing.
'0' gives the ASCII value 48.
If the computer on which you are working on doesn't use ASCII ( ie '0' != 48 ) the program may not work anyway
Use '0' instead of 48. Even EBCDIC lists digits contiguously. :->
Ah thanks for clearing that up.
So is there an advantage of using '0' versus 48?
as he said, it makes it more usable on different computers. Some computers or more to the point some operating systems may not use ascii so using '0' will still work on those types of os's.

as for the converted interger being -539222987 the reason is, integers only go up to a few million or couple billion, again this is computer specific (32bits lower than 64bit machines), you can find the max integer for the computer you are working on by using INT_MAX, but typically what happens is once you pass the maximum number then it rolls back around to the negative as it cannot go any higher, so say 289,382,102 is the highest int you can have, adding +1 = -289,382,101, +1 = -289,382,100 and so on, until you get back up to +289,382,102...

going from memory (and assuming it's the same as java, i can't quite remember).
integers are only 4bytes. to use really big numbers you want to use 'longs' which are big ints. These are 8bytes. and go up to a few billion trillion. 19 numbers in length. eg.
9,234,567,890,123,456,789 to -9,234,567,890,123,456,789
Last edited on
So is there an advantage of using '0' vurses 48?

Technically, maybe.
Morally and ethically, yes.

The compiler will convert '0' to the proper value no matter what character encoding the target system uses (the system where your program is being compiled). In reality, that is either ASCII (the vast majority of them) or EBCDIC (some few old, old, pieces of junk still in use).

But in any case, the '0' is much more readable than some magic "48": it makes much more clear your purpose in writing. Compare:
1
2
3
4
5
6
7
int digit2int( char digit )
  {
  if ((digit >= '0') && (digit <= '9'))
    return digit - '0';
  else
    return -1;
  }
1
2
3
4
int flobbitz( char c )
  {
  return ((c >= 48) && (c <= 57)) ? (c - 48) : -1;
  }

Hope this helps.
[edited cause i'm too darn slow]
Last edited on
Pages: 12