C++ Program to insert comma at appropriate position in an entered (max-15) digit number as per the mathematical theory
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void AddCommas( char * In, char * Out );
int main()
{
clrscr();
char Input[17];
char Output[21];
printf("Please enter a number (max 15 char) ");
cin.get(Input,16);
AddCommas(Input, Output);
cout<<Output<<endl;
system("pause");
getch();
return 0;
}
void AddCommas( char * In, char * Out )
{
int nLength = strlen(In);
int incr;
int nCount;
int nPosition = 0;
if( (nLength % 3) == 0 )
nCount = 0;
else
{
if( ((nLength % 3) == 1) && (In[0] == '-') )
nCount = -1;
else
nCount = 3 - (nLength % 3);
}
for ( incr = 0; incr < nLength; incr++)
{
if( nCount == 3 )
{
Out[nPosition] = ',';
nPosition++;
nCount = 0;
}
Out[nPosition] = In[incr];
nPosition++;
nCount++;
}
Out[nPosition] = 0; //terminating zero
}
Related Posts : C++ Programs
0 comments:
Post a Comment