Basic Currency Converter in C – Dead Handy, Innit

basic currency converter in c – dead handy, innit

intro

Oi, you lot! New to C and fancy summat a bit useful? We’re gonna sling together a lil program to flip cash from one currency to another—like quid to dollars or euros. It’s dead simple but proper cracking—like when I was tryna work out how many pints I could buy with my holiday euros (messed it up, too smashed). Great for getting yer head round basic maths and input in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together with hard-coded rates (real ones change, mind!). Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:

#include

float convert(float amount, int from, int to) {
float rates[]={1.0, 1.2, 0.85}; // GBP, USD, EUR (GBP base)
float inGBP=amount/rates[from-1]; // flip to GBP first
return inGBP*rates[to-1]; // then to target
}

int main() { // nearly did void, ha!
float amount; // cash bucket
int from,to; // which currencies

printf("gimme amount: ");
scanf("%f",&amount);
printf("convert from (1=GBP, 2=USD, 3=EUR): ");
scanf("%d",&from);
printf("convert to (1=GBP, 2=USD, 3=EUR): ");
scanf("%d",&to);

if(from<1||from>3||to<1||to>3||amount<0) { // no daft stuff
printf("oi, gimme proper options (1-3) and amount (0+)!\n");
}
else {
float result=convert(amount,from,to); // crunch it
char* currencies[]={"GBP","USD","EUR"};
printf("%.2f %s is %.2f %s\n",amount,currencies[from-1],
result,currencies[to-1]); // forgot ; once, ugh!
}

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I tested it with a couple conversions—worked bang on, mate! (Rates are fake—1 GBP = 1.2 USD, 0.85 EUR—real ones shift, mind!)

gimme amount: 10
convert from (1=GBP, 2=USD, 3=EUR): 1
convert to (1=GBP, 2=USD, 3=EUR): 2
10.00 GBP is 12.00 USD

gimme amount: 20
convert from (1=GBP, 2=USD, 3=EUR): 2
convert to (1=GBP, 2=USD, 3=EUR): 3
20.00 USD is 14.17 EUR

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!

1. that `#include ` gubbins
This is me nicking my toolkit. ``—fancy pants “standard input-output”—lets me use `printf` and `scanf`. Forgot it once, and my code just sat there like a plonker. Total mug move!

2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!

3. boxes: `float amount;` and pals
These are my lil buckets—`amount` for the cash ya chuck in, `from` and `to` for the currency picks (1=GBP, 2=USD, 3=EUR). `float` coz money’s got decimals, innit!

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme amount: ")` is me barking, “Oi, cash, sharpish!” Then `scanf("%f",&amount)` grabs it. `printf("convert from...")` and `scanf("%d",&from)` nab the starting currency, same for `to`. `%f` for floats, `%d` for ints, `&` like a post-it saying “bung it here, ya twit!”

5. `convert` funtion
Here’s the guts! `convert(float amount, int from, int to)` does the flip. `rates[]={1.0,1.2,0.85}` holds fake rates—GBP base (1.0), USD (1.2), EUR (0.85). `inGBP=amount/rates[from-1]` flips it to GBP first, then `inGBP*rates[to-1]` flips it to the target. Like swapping quid to dollars via the bar tab!

6. check the daft ones: `if(from<1||from>3...)`
If ya chuck in dodgy picks or negative cash—`if(from<1||from>3||to<1||to>3||amount<0)`—it yells “oi, proper options only!” Keeps the riff-raff out!

7. chucking it out: `printf` and `convert`
`float result=convert(amount,from,to)` crunches it, then `printf("%.2f %s is %.2f %s\n",...)` shows it—`%.2f` for two decimals, `currencies[]` names em (GBP, USD, EUR). Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

8. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to convert currencies. Faff with it—chuck in daft amounts, tweak it to say “pint cash” if ya fancy. First time I ran this, I flipped 5 quid to dollars and got 6—bang on for a toy job, eh! You’re a star already, mate—keep smashing it! 😊 (Real rates change, mind—this is just pub maths!)