Thread: Basic C Help - Currency Converter

1. Basic C Help - Currency Converter

Write a computer program in C which will request the user to enter a number representing the amount of foreign currency he/she would like to exchange. Your program should output a foreign currency conversion table for at least 6 different currencies, showing how much of each type of foreign currency can be obtained for the amount the user enters.

The example below shows a foreign currency exchange table for one unit of foreign currency. Your program is interactive and so should work for any input amount from the user not just 1.00. In the example table, the number entered by the user is 1, and it shows that 1 USD is equal to 0.547525 GBP, while 1 GBP is equal to 2.11928 AUD and so forth. If the user enters 2, your table should show how much 2 USD is in GBP, CAD, EUR, etc. along with how much 2 CAD would be in EUR, GBP, USD, etc, for each of the 6 currencies. The example uses real graphics not ASCII-art so it is ok if your table is not as visually pleasing.

So far this is what I've done:

Code:
```/*
Takes currency input from user and outputs currency conversions for USD, GBP, CAD,
EUR, AUD, CYN (Chinese Yuan)
*/

#include <stdio.h>
#include <stdlib.h>

main()
{

/* Declare and Initialize Variables */
const float USD = 1.0000;
const float GBP = 0.5653;
const float EUR = 0.7006;
const float AUD = 1.2335;
const float CYN = 6.8400;
const float UserInput = '\0';

/* Begin user interaction */
printf("Please input the amount of currency you wish to convert: ");
scanf("%d", &UserInput);
printf("\n");
printf("\n");

printf("|----------------------------------------------------|\n");
printf("|        | USD | GBP | CAD | EUR | AUD | CYN  |\n");
printf("|----------------------------------------------------|\n");
printf("| USD |        |        |        |        |        |         |\n");
printf("|----------------------------------------------------|\n");
printf("| GBP |        |        |        |        |        |         |\n");
printf("|----------------------------------------------------|\n");
printf("| CAD |       |        |        |        |        |          |\n");
printf("|----------------------------------------------------|\n");
printf("| EUR |        |        |        |        |        |          |\n");
printf("|----------------------------------------------------|\n");
printf("| AUD |        |        |        |        |        |         |\n");
printf("|----------------------------------------------------|\n");
printf("| CYN |        |        |        |       |         |          |\n");
printf("|----------------------------------------------------|\n");

system("PAUSE");
return 0;
}```

All the "|" should line up obviously to make it look like a table, but the forum formatted it oddly.

The basic gist of the program is that the user will input a currency amount (in \$US) they wish to convert. After they input the currency amount, the program is supposed to output that table with all the conversions. It should output the entire table every time they put in a currency amount to convert. Inside the actual cells, should be a number going two decimal places (I'm aware it's %.2f to get that).

Anyway, I don't even know if I've done everything up to this point correctly. But if I have, my next step (I assume) would be to do the actual conversions. And I don't particularly know how to do that or what to do. Would I do the conversions within each individual cell in the table? How would I do that? Or would I do them somewhere outside of the table and store them as a variable of some sort and then input that variable into the table somehow?

If someone could give an example of how they would do the first row (USD across) or describe how they would do it, that would be great.

Any help/direction/corrections is welcome and very much needed. I know it's not hard, but this is the very first bit of programming I've ever done so...

Thanks in advance for any help at all.

2. Well, I think the best way to solve this problem would be with an array of struct (each element representing a "currency"). Else, this is going to be painful to implement. Painful, inefficient, inelegant, repetitive, error-prone, etc,... But if you do have to do without array, you'll need to write code for every conversion, which will be extremely repetitive.

Code:
```struct Currency
{
char *currencySymbol;
double equivInUSD;
};

int main()
{
// Variables declaration
const struct Currency currencies[] = {
{"USD", 1.0},
{"GBP", 1.0 / 0.5653}
...
};
const size_t nbCurrencies = sizeof(currencies) / sizeof(*currencies);
...

...

...

// Do the conversion and print
...
}```
Also, are you sure that "the user will input a currency amount (in \$US) they wish to convert.". If it's the case, then the table doesn't make a lots of sense; you would only need a line, not a table.

Also,
Code:
`scanf("%d", &UserInput);`
is wrong if UserInput is of type float. The correct specifier for float is "%f".
http://www.cplusplus.com/reference/c...dio/scanf.html

3. Just knocked this up. I'm pretty new to programming, been learning C for the past few weeks.

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

int main()
{
char usdi[32];
float usdi2;
const float gbp = 0.5653;
const float eur = 0.7006;
const float aud = 1.2335;
const float cyn = 6.8400;

printf("Please enter the amount, in USD, that you wish to convert: ");
gets(usdi);
usdi2=atof(usdi);

printf("\nFor &#37;.2f USD you would get %.2f GBP.\n",usdi2,usdi2*gbp);
printf("For %.2f USD you would get %.2f EUR.\n",usdi2,usdi2*eur);
printf("For %.2f USD you would get %.2f AUD.\n",usdi2,usdi2*aud);
printf("For %.2f USD you would get %.2f CYN.\n",usdi2,usdi2*cyn);

return(0);
}```

4. 1. Don't use gets() -- see the FAQ

5. This better?

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

int main()
{
float usd;
const float gbp = 0.5653;
const float eur = 0.7006;
const float aud = 1.2335;
const float cyn = 6.8400;

printf("Please enter the amount, in USD, that you wish to convert: ");
scanf("&#37;f",&usd);

printf("\nFor %.2f USD you would get %.2f GBP.\n",usd,usd*gbp);
printf("For %.2f USD you would get %.2f EUR.\n",usd,usd*eur);
printf("For %.2f USD you would get %.2f AUD.\n",usd,usd*aud);
printf("For %.2f USD you would get %.2f CYN.\n",usd,usd*cyn);

return(0);
}```

6. Sure is, but you should be aware (not a problem in this case), that scanf() can be troublesome:

There are two problems with using scanf() to get a number:

First, validation/error handling is poor. If the user enters 1234ZZ as their number, scanf() will read 1234 into the int variable, and leave the ZZ in the input buffer. Although the user has entered what appears to be an invalid number, the program hasn't actually noticed, and will continue processing as if all is well.

The second problem is that of leaving characters in the buffer, in particular the newline character tends to catch people out. To show what I mean, here is some sample code that uses scanf() to get a number, followed by fgets() to get a string. As you may of heard already, fgets() is designed for reading strings, but cannot read numbers directly like scanf() can.

7. A different approach would be using fgets(), which is more safe than gets().

8. Originally Posted by mmcg
This better?

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

int main()
{
float usd;
const float gbp = 0.5653;
const float eur = 0.7006;
const float aud = 1.2335;
const float cyn = 6.8400;

printf("Please enter the amount, in USD, that you wish to convert: ");
scanf("%f",&usd);

printf("\nFor %.2f USD you would get %.2f GBP.\n",usd,usd*gbp);
printf("For %.2f USD you would get %.2f EUR.\n",usd,usd*eur);
printf("For %.2f USD you would get %.2f AUD.\n",usd,usd*aud);
printf("For %.2f USD you would get %.2f CYN.\n",usd,usd*cyn);

return(0);
}```
Thanks for the help.

My biggest question is how do I output the calculations into a table as shown in my original post? The calculations aren't really my problem, it's doing the calculations and getting the conversions to show up in good form in my table.

9. Originally Posted by liljp617
Thanks for the help.

My biggest question is how do I output the calculations into a table as shown in my original post? The calculations aren't really my problem, it's doing the calculations and getting the conversions to show up in good form in my table.
The "f" in "printf" is supposed to stand for "formatted" -- you give it a string that says how the output is supposed to look, and it fills it in:
Code:
`printf("|   USD|------|%6.2f|%6.2f|%6.2f|%6.2f|%6.2f|\n", var1, var2, var3, var4, var5);`
is going to print all the characters literally except those between the % and the f, which will get replaced by the corresponding variable on the right in 6.2 format. (Note also they don't have to be variables, they can be expressions or the results of function calls.)

10. Thanks a lot :P I actually just figured it out minutes before reading the above post. Thanks a lot guys, you'll probably be hearing from me a lot in the future. Very helpful.