1

I am working on a project for my computer science course that is focusing on the C language, and I almost almost done, I just can't figure out why the currency convert function at the bottom drops some of the zeros in the numbers it prints. I am pretty new to C, and learning as I go, so I apologize if this code is not optimized and/or pretty for experienced eyes. Any suggestions on changes are welcome. Below is example print to show my problem, and my code.

Please enter seed number:  5

  Running 26 Times
     7374270.50 to Dollars:  $7,374,270
     -643185.56 to Dollars: -$643,185
     7907977.50 to Dollars:  $7,907,977
     -810537.94 to Dollars: -$810,537
    -3670141.50 to Dollars: -$3,670,141
    -4847146.50 to Dollars: -$4,847,146
    -1805439.50 to Dollars: -$1,805,439
    -7718499.50 to Dollars: -$7,718,499
      209791.62 to Dollars:  $209,791
    -3349182.00 to Dollars: -$3,349,182
     7803616.50 to Dollars:  $7,803,616
     7607553.50 to Dollars:  $7,607,553
     -232233.81 to Dollars: -$232,233
     4807206.50 to Dollars:  $4,807,206
    -4712548.00 to Dollars: -$4,712,548
     -253349.20 to Dollars: -$253,349
    -7257441.50 to Dollars: -$7,257,441
    -5146261.50 to Dollars: -$5,146,261
     -324208.03 to Dollars: -$324,208
     3437480.75 to Dollars:  $3,437,480
     -295008.59 to Dollars: -$295,8
      118086.98 to Dollars:  $118,86
     1243459.88 to Dollars:  $1,243,459
     5947147.50 to Dollars:  $5,947,147
     3280724.00 to Dollars:  $3,280,724
    -2176277.75 to Dollars: -$2,176,277   

> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
> #include <time.h> /*extra includes are allowed (and required)*/
> 
> void currencyConvert(int money, int cents); void
> PrettyPrintMoney(float money, int cents); /*extra functions are
> allowed to be declared*/
> 
> const int MAX_NUMBER_OF_LOOPS = 30; //do not change this line const
> int MIN_DOLLAR_AMOUNT = -8000000; //do not change this line const int
> MAX_DOLLAR_AMOUNT =  8000000; //do not change this line const int
> TOTAL_LENGTH = 15; //do not change this line
> 
> int main(void) {   unsigned int intSeed = 0;   unsigned int intLoops =
> 0;   int randGenerated = 0;   int money = 0;   int cents = 0;   time_t
> t;   /*extra variables are allowed to be defined*/    printf("Please
> enter seed number: ");    scanf("%u", &intSeed);
>    
>       srand(intSeed);
>       intLoops = rand()%30 + 1;    printf("\n  Running %d Times", intLoops);
>        //printf("\n%u", intLoops); srand(t + intLoops);
> 
> while (intLoops > 0){
>      randGenerated = rand() % 16000001 + (-8000000);   cents = rand() % 100;   money = randGenerated;   PrettyPrintMoney(money, cents);  
> currencyConvert(money, cents);   intLoops = intLoops -1; 
>      }   return 0; } //end main void PrettyPrintMoney(float money, int cents)//float fltMoneyIn) { ; if (money < 0 && money < -999999){  
> printf("\n%15.f.%02d to Dollars: -$", money, cents); } if (money < 0
> && money > -999999){   printf("\n%15.f.%02d to Dollars:   -$", money,
> cents); } if (money > 0 && money > 999999){   printf("\n%15.f.%02d to
> Dollars:  $", money, cents); } if (money > 0 && money < 999999){  
> printf("\n%15.f.%02d to Dollars:    $", money, cents); }   return; }
> //end PrettyPrintMoney
> 
> // Change money to currency (commas) void currencyConvert (int
> money,int cents){
>       int order_of_magnitude = (money == 0) ? 1 : (int)pow( 10, ((int)floor(log10(abs(money))) / 3) * 3 ) ;
>   
>       printf( "%d", abs(money / order_of_magnitude ), cents) ;
>   
>     for( money = abs( money ) % order_of_magnitude, order_of_magnitude /= 1000;
>         order_of_magnitude > 0;
>         money %= order_of_magnitude, order_of_magnitude /= 1000 )
>         {
>         printf( ",%03d", abs(money / order_of_magnitude) ) ;
>         
>     } printf(".%02d", cents);    }
>   
> 
> 
> /*extra functions are allowed to be defined*/
Ether_904
  • 11
  • 2
  • 10
    A standard warning: **[Do not](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)** use floating point representation for currency. – Eugene Sh. Feb 20 '18 at 20:18
  • 2
    Take @EugeneSh. advice seriously, you can write 500000 lines of code and then realize that you screwed everything because you used floating point for currency. Well maybe you wont, but I did and it was really sad. – Iharob Al Asimi Feb 20 '18 at 20:24
  • 1
    @EugeneSh. Understood, will fix accordingly. Thanks for the link + advice – Ether_904 Feb 20 '18 at 20:27
  • 1
    Thanks for the help user3121023, that helped a ton, I'm as close to being done as i've been so far. Updating code section on the original post. – Ether_904 Feb 20 '18 at 21:22

0 Answers0