I was trying to make a currency converter using C language and it was really easy to do, but I was trying to readjust it by using functions because I wanted to rewrite the code each time the user enters a wrong input. Here's my code:
#include <stdio.h>
char conv;
int convert()
{
if (conv == 'd')
{
float i;
printf("type the amount in dollars: ");
scanf("%f", &i);
float egp = i * 15.72;
printf("Your amount in egp is: %.2f", egp);
}
else if (conv == 'e')
{
float i;
printf("type the amount in egyptian: ");
scanf("%f", &i);
float egp = i / 15.72;
printf("Your amount in dollars is: %f", egp);
}
return 0;
}
int error()
{
return main();
}
int main()
{
printf("\nPlease write {d} to converts dollars to egp, type {e} to convert egp to dollars: ");
scanf("%c", &conv);
if (conv == 'd' || conv == 'e')
{
convert();
}
else
error();
return 0;
}
It works like a charm because I spent a good amount of time on it, but the thing is it rewrites the code twice; I don't know what's causing this, I'm pretty sure there's something missing in my function logic and my understanding of functions in c. Go ahead try it out.