GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Feb-2004, 15:02
Tealion Tealion is offline
New Member
 
Join Date: Feb 2004
Posts: 5
Tealion is on a distinguished road
Question

Integer splitting?


Can anybody help me with this problem? Or is there a better way of doing it then I am thinking of?

I want to split up an integer into each individal unit and then store it in an array. Basically I need to get the user to enter a whole number and then put each individual unit of that number in a separate variable. Can anybody help??

Thankyou
  #2  
Old 08-Feb-2004, 15:17
DWk DWk is offline
Junior Member
 
Join Date: Feb 2004
Location: Guatemala
Posts: 38
DWk is on a distinguished road
Cast it into a string, then store the string in an array, and then access each array position?
  #3  
Old 08-Feb-2004, 15:20
Tealion Tealion is offline
New Member
 
Join Date: Feb 2004
Posts: 5
Tealion is on a distinguished road
Question

Quote:
Originally Posted by DWk
Cast it into a string, then store the string in an array, and then access each array position?

Wouldn't the array then contain chars though? I forgot to mention that I wanted the array to contain integers.
  #4  
Old 08-Feb-2004, 15:26
DWk DWk is offline
Junior Member
 
Join Date: Feb 2004
Location: Guatemala
Posts: 38
DWk is on a distinguished road
AFAIK, you CAN store numbers
  #5  
Old 08-Feb-2004, 15:31
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Tealion
Wouldn't the array then contain chars though? I forgot to mention that I wanted the array to contain integers.

Hey, I think DWK may have a pretty good idea here. If you do an atoi call on it and get an array of chars, then go through the array and subtract each number by 48, you will have the digits.

I have a function that you can modify to do it directly, but I think it will be much easier doing it DWK's way.

My function is an atoi replacement and you could modify that not to convert the digit into a char. Let me know if you still want to take a look at it.
  #6  
Old 08-Feb-2004, 15:36
DWk DWk is offline
Junior Member
 
Join Date: Feb 2004
Location: Guatemala
Posts: 38
DWk is on a distinguished road
But you are doing it ASCII way, and I assume the way of converting would be much faster (and easier)

(By the way....I got stuck again, dsmith
so if you are kind drop by my post )
  #7  
Old 08-Feb-2004, 16:12
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Tealion, I am afraid that I gave you some bad advice The function that you want is actually itoa and it doesn't exist in a lot of compilers. Here is a function that I wrote that converts an integer to a string (char *). You just need to modify it so that it doesn't adjust for ascii and so that it returns an integer array.

CPP / C++ / C Code:
char *numasc(int num)
{
	char	string[30],
			*ret;
	int		digit,
			left,
			pos,
			negative = 0;


	if(num == 0){
		ret = new char[2];
		*ret = '0';
		*(ret+1) = 0;
		return ret;
  	}
	string[29] = 0;
	left = num;
	pos = 28;
	if(left < 0){
		left = abs(left);
		negative = 1;
	}

	while((left) && (pos)){
		digit = left % 10;
		left = (int) left/10;

		string[pos] = digit+48;
		pos--;
	}
	if(negative){
		string[pos] = '-';
		pos--;
  	}
    ret = new char[30-pos];
	strcpy(ret,&string[pos+1]);

	return(ret);
}

Its kind of ugly and for what you want to do, can be cut down considerably. If you have any trouble with this let me know...
  #8  
Old 08-Feb-2004, 21:19
DWk DWk is offline
Junior Member
 
Join Date: Feb 2004
Location: Guatemala
Posts: 38
DWk is on a distinguished road
Oh yes it can be cut down :S

I think all you need is 2 cycles:

one that fits the number into the array and casts it, and the other that just returns them one by one
__________________
Just trying to be a programmer :P
  #9  
Old 09-Feb-2004, 08:17
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by DWk
Oh yes it can be cut down

Actually, it can't be cut down that much. You need to think about how variables are really stored in C. If I do a typecast of:
CPP / C++ / C Code:
char* string = new char[20];
int number;

string = (char *) number;
All that will do is point my string to the memory location defined by number.

If I do:
CPP / C++ / C Code:
*string = (char) number;
That will change the number to a char (in most cases no different) and place it as the first charecter in your string.

So, in order to place an integer into a charectar (or digit array), you must do something like I posted. Now, some compilers have a function called itoa that will convert an integer to an ascii string, which is the inverse of the itoa. And there may be other more elegant approaches than mine (I'm sure of it.) But no matter what language you use, somewhere in the background there are functions like these that make it appear that you are doing a typecasting when you are really not.

Now, the stuff that can be taken out of the routine for what tealion is doing is the negative tracking (I assume), the conversion from an integer to an ascii (adding by 48 ) and the final placement into a properly sized string. Then change it to return a integer array instead of a char *. Once again, Tealion, if you want to use this, but are struggling with modifiying it, let me know.
  #10  
Old 09-Feb-2004, 11:58
Tealion Tealion is offline
New Member
 
Join Date: Feb 2004
Posts: 5
Tealion is on a distinguished road
Unhappy

Quote:
Originally Posted by dsmith
Actually, it can't be cut down that much. You need to think about how variables are really stored in C. If I do a typecast of:
CPP / C++ / C Code:
char* string = new char[20];
int number;

string = (char *) number;
All that will do is point my string to the memory location defined by number.

If I do:
CPP / C++ / C Code:
*string = (char) number;
That will change the number to a char (in most cases no different) and place it as the first charecter in your string.

So, in order to place an integer into a charectar (or digit array), you must do something like I posted. Now, some compilers have a function called itoa that will convert an integer to an ascii string, which is the inverse of the itoa. And there may be other more elegant approaches than mine (I'm sure of it.) But no matter what language you use, somewhere in the background there are functions like these that make it appear that you are doing a typecasting when you are really not.

Now, the stuff that can be taken out of the routine for what tealion is doing is the negative tracking (I assume), the conversion from an integer to an ascii (adding by 48 ) and the final placement into a properly sized string. Then change it to return a integer array instead of a char *. Once again, Tealion, if you want to use this, but are struggling with modifiying it, let me know.

I need help!
It also needs to be in C, not C++
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.