objective c - Numbers into words issue C -
I am trying to write a program that will take an integer input, and then convert it into words. For example: 123, one two three plus 398, negative three nine zero eight, my code works 90% of the time, the only issue is coming up when I put one or more zeros at the end of the integer I am like. 70800 will come in seven void eight as it completely misses the end zero. I understand why this is happening, but someone knows that there is no way around it. PS (I do not have permission as part of this function because the input is accepted as string and divided into array, it would be best if answer is based on this code).
Int main (Int'Argic, Const four * AGRV []) {@autoreleasepool {float abNumber; Int i = 0; float number; Float result; Float first number; Printf ("Type a Type:"); Scanf ("% f", and first number); Abnumber = abs (first number); If (first number & lt; 0) {printf ("negative"); } Number = ABNumber; Whereas (number> = 10) {number = number / 10; I ++; } Do {float count number = abnumber; Float power = pawf (10, -i); Float power no 2 = pawf (10, i); CountNumber = Count Number * Power; Results = floor (countNumber); If (results == 9) {printf ("nine"); } If (results == 8) {printf ("eight"); } If (results == 7) {printf ("seven"); } If (result == 6) {printf ("six"); } If (result == 5) {printf ("five"); } If (result == 4) {printf ("four"); } If (result == 3) {printf ("three"); } If (result == 2) {printf ("two"); } If (result == 1) {printf ("one"); } If (result == 0) {printf ("zero"); } While (abnumber> power no2) {abNumber = abNumber - powerNo2; } I--; } While (i> = 0); } Return 0; }
The main error is that
(Abnumber & gt; = PowerNo2) {
should be
while (abNumber> = powerNo2) {
But I suggest that to avoid possible spherical errors, do not use floating digit arithmetic at all. The same can be obtained with integer arithmetic (I have left "negative case" for simplicity):
int numbers; Printf ("Type a Type:"); Scanf ("% d", and number); // Set the highest power of 10 which is & lt; = Given number: int power = 1; While (10 * power & lt; = number) {electricity * = 10; } // Remove each number: while (power> gt; 0) {int digit = (number / power)% 10; / * * Use switch / case to print 'digit' as string ... * / power / = 10; }
Comments
Post a Comment