hi..i am trying to do a currency converter in C..
but there are errors that i can't figure out....
and it don't accept negative numbers and characters...but mistake here..can anyone help?
is there any simplified version or other C examples of a converter please...
Code:
#include<stdio.h>
/* Conversion rates */
float rate_peso = 0.787746;
float rate_franc = 1.23073;
float rate_euro = 0.781138;
float rate_kroner = 6.77551;
float rate_rand = 6.62313;
/*List functions*/
void welcome();
void result1(float num_usd);
void goodbye();
float pesoToUSD(float rate_peso);
float inputamount();
main()
{
/* Define variables */
float peso;
float num_usd;
/* Program execution */
welcome();
peso = inputamount();
num_usd = pesoToUSD(peso);
result1(num_usd);
goodbye();
return 0;
}
void welcome()
{
printf("\nCurrency Conversion\n");
}
/*Prompts for the number of Pesos to convert*/
float inputamount()
{
/* local variables */
float temp = 1.0; //Holds the return value
char Ch; //input and error checking variable
char *input; //Holds input string for conversion
int isGood = 0; //Boolean 0 is 'false', 1 is 'true'
int x = 0; //for loop counter
int dec_count = 0; //counts number of decimals
/*The do-while loop*/
do
{
/* Input*/
printf("\nEnter a positive number of Pesos to convert to USD.\n");
gets (input);
for(x=0; x < strlen(input); x++)
{
//Check each character in input string.
//ASCII values 48-57 are the digits. (Win32)
if (((input[x] >= 48 && input[x] <= 57) || (input[x] == '.' && dec_count == 0)))
{
isGood = 1; //indicates that input is valid
}
else
{
printf("\a"); //audible error signal
isGood = 0; //Repeat the do-while
break; //breaks the for loop
}
if (input[x] == '.')
dec_count++;
}
}while(isGood == 0);
sscanf(input, "%f", &temp); //reads the input string into a float
return temp;
}
float pesoToUSD(float peso)
{
float temp;
temp = peso/rate_peso;
//printf("\npeso=%f rate=%f temp=%f\n", peso, rate_peso, temp);
return temp;
}
/*output*/
void result1(float num_usd)
{
printf("\n\nThe amount of Pesos you entered is equal to $%.2f USD.\n", num_usd);
}
void goodbye()
{
}