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.
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
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.
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.
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 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:
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.
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
'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
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
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: