i think it is suit for you
I think It may help you:
- There are a big difference between number and number in character.
- There are a big difference between string terminated by zero and a character.
:rolleyes:
Code:
//sort number
//mybe algorithm is Bubble Sort
//a bruce force algorithm
//optimized for check input buffer
#include <iostream>
using namespace std;
//convert from a character to a number
int chartoint(char c)
{
char buffer[2];
buffer[1]=0;//string need to be terminated by ZERO
buffer[0]=c;
return atoi(buffer);
}
//use to swap
void c_swap(char &a, char &b)
{
char buffer;
buffer=a;
a=b;
b=buffer;
}
//check "number" is number or not
bool checknumberstring(char*s)
{
int l = strlen(s);
for (int i=0; i<l+1;i++)
if (s[i]<'0' && s[i]>'9')
return false;
return true;
}
//main here
#define NUMBER 10
int main()
{
char initialdigit [NUMBER];
unsigned int firstinteger;
cout<<"Please enter a non-zero digit number..."<<endl;
fgets(initialdigit, NUMBER, stdin);
// check input data here
if (strlen(initialdigit)!=NUMBER-1 || checknumberstring(initialdigit)==false)
{
cout << "Error! Input..." << endl;
system("PAUSE");
return 1;
}
firstinteger = atoi (initialdigit);
if (firstinteger > 0)
{
for (int i=0; i<NUMBER-1; i++)
for (int j=0; j<NUMBER-1; j++)
if (chartoint(initialdigit [j]) < chartoint(initialdigit [j+1]))
c_swap(initialdigit [j], initialdigit [j+1]);
}
cout<<initialdigit<<endl;
system("PAUSE");
return 0;
}