-1

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.

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.

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.

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!

// This program compares a specified quantity of US Dollars to Czech
// Korunas at two different fixed exchange rates.

#include <stdio.h>
#include <math.h>

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", &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;

}  
Sinistar
  • 3
  • 3
  • With `dollars = floor (diff);`, consider the effect if `diff == 123.999...` where _mathematically_, 124 is expected. – chux - Reinstate Monica Jul 16 '18 at 20:21
  • 1
    Well, it's a poor teacher who lets you use `float` but not `double`. Always use `double` outside class unless there are very good reasons not to (such as MPU or library interface). Anyway don't your allowed functions take and return `double`? Also please see [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Weather Vane Jul 16 '18 at 20:23

1 Answers1

0

please replace

fprintf(diskfile,"The difference is %d.%.f U.S. dollars.\n", dollars, cents);

with this EDIT:

fprintf(diskfile, "The difference is %.2f U.S. dollars.\n", dollars+cents/100);
ES2018
  • 438
  • 3
  • 15
  • This makes the formatting on the 5000 input look good, but the output values are incorrect and the output for 30000 is also then malformatted. Thanks for the suggestion regardless. – Sinistar Jul 16 '18 at 21:21
  • I will post the solution in a minutes – ES2018 Jul 16 '18 at 21:36
  • Use this - fprintf(diskfile, "The difference is %.2f U.S. dollars.\n", dollars+cents/100); I just edit the answer – ES2018 Jul 16 '18 at 21:55
  • @Sinistar Are you happy with the suggestion, or do you prefer a different solution? – ES2018 Jul 18 '18 at 08:16