converting a char array into an int array

I have an a char array containing 5 6 7 8. I need to perform sorting function on the elements of the array (i have to arrange them in ascending order), but i can't unless they are numeric. how can convert this array into an int array? general outline would help
If you're only dealing with single-digit numbers, then a switch ( char ) { case '0': ... } etc would be the easiest way to do this, I think.
1
2
3
4
5
6
const int BAD_RETURN = 10;

inline int ConvertCharToInt(char ch)
{
  return((ch >= '0' && ch <= '9') ? ch - '0' : BAD_RETURN);
}


To be honest, though, you can still sort the array simply by checking the ASCII value. '5' < '6' < '7' < '8'.
Last edited on
Kiana, hmm. I don't really understand your code. What is the significance of defining a constant varialbe bad_return, and why are you initializing it to 10. And a little details on the third statement would definitely help. I am trying to understand the procedure, not rip someone's code off and paste it in my homework and turn it in as my own. Thank you
Basically a digit will always be between 0 and 9 so 10 would be a good error-code. We return it if the character is invalid (not inclusively between '0' and '9').

I'm not sure what you mean by the third statement but the most complicated thing in there looks like:

return((ch >= '0' && ch <= '9') ? ch - '0' : BAD_RETURN);

That's just using a ternary operator - the (A ? B : C) syntax means "if A, use B, else, use C". Therefore, it would be equivalent to:

1
2
3
4
if(ch >= '0' && ch <= '9')
  return ch - '0';
else
  return BAD_RETURN;
Last edited on
I'd suggest using exceptions rather than "magic numbers" in the code, as it makes it more readable (imho). There is an exception in C++ std::out_of_range that suites this, and would prevent problems in future if the people using this function didn't know that it only accepted single digits. Unlikely for this particular example, but IMO it's a better style generally.
I have an a char array containing 5 6 7 8. I need to perform sorting function on the elements...

What's wrong with the standard sort?
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <iostream>

int main()
{
	char array[] = { '7', '6', '5', '8' };
	std::sort(array, array + sizeof(array));
	for (char* p = array; p != array + sizeof(array); ++p)
		std::cout << *p << std::endl;

	return 0;
}
Last edited on
Kiana, thank you for breaking it down for me. That is definitely comprehensible now. Does the inline function have to be in main? i am actually not familiar with inline functions, so in case the inline function exists outside the main program how do i call it and whats the function prototype gonna look like? I will try it and keep you updated.

Cheers
Last edited on
kbw, appreciate your input. I am, however, required to convert the array into an int array. I know many sorting algorithms work on strings too, but an int array is what i am looking for.
My solution only converts numerical ASCII characters into their respective integers. If you're still trying to sort them then take a look at kbw's example.
but i can't unless they are numeric

A char is an ordered integral type. You don't need to convert to an int to sort. My example demonstrates that. Did you run it?
Last edited on
Topic archived. No new replies allowed.