I’m trying to put an integer into a string by separating its digits and putting them by order in a string of size 3
this is my code:
char pont[4];
void convertInteger(int number){
int temp100=0;
int temp10=0;
int ascii100=0;
int ascii10=0;
if (number>=100) {
temp100=number%100;
ascii100=temp100;
pont[0]=ascii100+48;
number-=temp100*100;
temp10=number%10;
ascii10=temp10;
pont[1]=ascii10+48;
number-=temp10*10;
pont[2]=number+48;
}
if (number>=10) {
pont[0]=48;
temp10=number%10;
ascii10=temp10;
pont[1]=ascii10+48;
number-=temp10*10;
pont[2]=number+48;
}
else{
pont[0]=48;
pont[1]=48;
pont[2]=number+48;
}
}
here’s an example of what’s suppose to happen:
number = 356
temp100 = 356%100 = 3
ascii100 = 3
pont[0]= ascii100 = 3
temp100 = 3*100 = 300
number = 365 - 300 = 56
temp10 = 56%10 = 5
ascii10 = 5
pont[1]= ascii10 = 5
temp10 = 5*10 = 50
number = 56 - 50 = 6
pont[2]=6
I might have an error somewhere and not seeing it (don’t know why) …
This is suppose to be C++ by the way. I might be mixing this up with C language…
Thanks in advance
Probably the mistake that you’re overlooking right now:
However, I’d like to suggest a different approach; you don’t care that the value is above
100,10, etc., as0is still a useful value — if you don’t mind zero-padding your answer.Consider the following numbers: