title: C: rounding and output formatting for dollars/cents in homework
body: <p>As part of my first homework assignment in an Intro to Dev class, I have to write a currency exchange program in C. I've met most of the assignment requirements, but I'm stuck on two parts. </p>
  
  <p>1) When I enter 30000 as the input value, I get 2 new values for the two exchange rates, but I'm then supposed to show the difference as the final output. When I do this, the rounding doesn't work out and the difference is one cent larger than the actual difference. I know this problem was covered in class, but even when I try to match the examples from the text (splitting up dollars and cents), I end up one cent off.</p>
  
  <p>2) When I enter 5000 as the input value, the final difference comes out as "16.9", but should have two digits after the decimal point. No matter how I format the output, I can't get something that shows accurate formatting on both the entry for 5000 and the entry for 30,000.</p>
  
  <p>Since this is our first assignment in an intro class, we can only use a few different arguments/commands. We aren't allowed to use double for anything. The instructions specifically mention using fabs() for absolute values in reporting the difference, which I've done here. I'm assuming at least the first problem is in my sequencing of fabs() and floor(), but being a newbie I'm pretty clueless about where to look for the actual problem. Any suggestions or assistance would be greatly appreciated! </p>
  
  <pre><code>// This program compares a specified quantity of US Dollars to Czech
  // Korunas at two different fixed exchange rates.
  
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int kor;                        // User-entered Korunas
  const float EXRATE1 = 42;       // Constant Exchange Rate 1
  const float EXRATE2 = 37;       // Constant Exchange Rate 2
  float dolex1;                   // User-entered Koruna value with Exchange Rate 1 applied
  float dolex2;                   // User-entered Koruna value with Exchange Rate 2 applied
  float diff;                     // Difference between two values
  int dollars;                    // Diff value expressed as absolute, dollar digit
  float cents;                    // Diff value expressed as absolute, cents digits
  
  FILE *diskfile;
  
  //Main function
  
  main(){
  
  // Get information from user
  
  printf("*** Koruna Exchange App ***\n\n");
  
  printf("How many korunas do you have in your savings account?\n");
  scanf("%d", &amp;kor);
  
  printf("The exchange information for %d korunas is now being recorded.\n", kor);
  
  // Combine user-entered value with various exchange rates to get new values
  
  dolex1 = kor / EXRATE1;
  dolex2 = kor / EXRATE2;
  
  // Find difference between new values, then get absolute value
  
  diff = fabs (dolex2 - dolex1);
  
  //Separate dollars and cents
  
  dollars = floor (diff);
  cents = (diff-dollars) * 100;
  
  //Open and create file
  
  diskfile = fopen("c:\\class\\test-ke.txt","w");
  
  //Display results
  
  fprintf(diskfile,"For %d korunas:\n", kor);
  fprintf(diskfile,"At the rate of %.0f korunas per U.S. dollar,\n", EXRATE1);
  fprintf(diskfile,"you have %.2f U.S. dollars.\n", dolex1);
  fprintf(diskfile,"At the rate of %.0f korunas per U.S. dollar,\n", EXRATE2);
  fprintf(diskfile,"you have %.2f U.S. dollars.\n\n", dolex2);
  
  fprintf(diskfile,"The difference is %d.%.f U.S. dollars.\n", dollars, cents);
  
  fclose(diskfile);
  
  return 0;
  
  }  
  </code></pre>