Currency Converter

This is a discussion on Currency Converter within the C Programming forums, part of the General Programming Boards category; Basic homework problem. I did all of this, and it works fine. It gives the right numbers in the right ...

1. Currency Converter

Basic homework problem. I did all of this, and it works fine. It gives the right numbers in the right format. And then I looked back at the problem statement and it said I was supposed to use if-else-if statements -within- the switch to do the actual conversions, which doesn't make much sense to me. All of the other parameters seem to work out, any suggestions on how to redo it with if-else-if?

Code:
```#include <stdio.h>

int main()
{
const double EUR = 1.2926;
const double GBP = 1.6186;
const double CLP = 0.0021;
const double CNY = 0.1584;
const double ZAR = 0.1208;

int code;
double eur;
double gbp;
double clp;
double cny;
double zar;
int count = 0;

printf("\nEnter country code, zero to end: ");
scanf("%d", &code);

while (code != 0)
{
switch (code)
{  case 1:
count = count + 1;
break;
case 2:
printf("Enter EUR: ");
scanf("%lf", &eur);
printf("%.2f EUR = ", eur);
eur = eur * EUR;
printf("%.2f USD\n", eur);
count = count + 1;
case 3:
printf("Enter GBP: ");
scanf("%lf", &gbp);
printf("%.2f GBP = ", gbp);
gbp = gbp * GBP;
printf("%.2f USD\n", gbp);
count = count + 1;
break;
case 4:
printf("Enter CLP: ");
scanf("%lf", &clp);
printf("%.2f CLP = ", clp);
clp = clp * CLP;
printf("%.2f USD\n", clp);
count = count + 1;
break;
case 5:
printf("Enter CNY: ");
scanf("%lf", &cny);
printf("%.2f CNY = ", cny);
cny = cny * CNY;
printf("%.2f USD\n", cny);
count = count + 1;
break;
case 6:
printf("Enter ZAR: ");
scanf("%lf", &zar);
printf("%.2f ZAR = ", zar);
zar = zar * ZAR;
printf("%.2f USD\n", zar);
count = count + 1;
break;
default:
printf("%d is an invalid country code!", code);
break;
}
printf("\nEneter country code, zero to end: ");
scanf("%d", &code);
}
printf("\n%d conversions done.\n\n", count);
return 0;
}```

2. I don't see any need to use more if-than-else statements. If you want to consider an even more compact program the 'switch' isn't necessary if you keep the conversion factors in an array and index it according to currency code. No need for separate variables 'cad', 'eur', 'gbp', etc.

3. I don't see a need for it either, which is why it's confusing. But that's the way the assignment wants it, if-else-if within the switch within the while loop. I don't know what variables I would even put into if-else-if statements for something that's just a conversion.

4. Can you post the problem statement.

5. 1. General:
1.1. Save your code as prog2.c.
1.2. Named constants must be used to represent the exchange rates. Use data type double to hold the conversion factors and currency amounts.
1.3. Follow the line spacing and format shown in the sample output. All currencies are printed to two decimal places.
2. main:
2.1. Defines named constants and then variables.
2.2. Priming prompt and read if required.
2.3. Use a while loop to perform the conversions as long as the country code is not zero. Inside the loop:
2.3.1. If the country code is invalid print the error message otherwise:
2.3.1.1. Use a switch statement to prompt for the currency amount.
2.3.1.3. Convert to USD using an if-else-if statement.
2.3.1.4. Print the result of the conversion using either an if-else-if or switch (if either required) for the currency designation.
2.3.2. Perform a subsequent prompt and read if required.
2.4. Print a blank line, the count of the number of conversions done, and an ending blank line.

6. Ahh, see, you did the prompting, converting, and printing all within a single "switch()" statement - which is a very good implementation.

But the assignment seems to require these be broken down into separate steps.

Code:
```switch(...)
// prompt for currency
if(...)
// convert to USD
switch(...)
// print result```

7. Oh, I see. Yeah, that makes more sense, I think I have it figured out now. Thanks! =]