Hello,
I am having an issue with a function I have written accepting input as a string and converting it to a float usint atof(). When it is run it accepts the string beutifully but does funky things upon conversion. I have printed out the variables at each step and after conversion my variable dbl_INPUTUSD is equal to "$f" not a number like I need. Here is the code from my function. Any help would be appreciated. I cannot spot the problem...
float fun_Input() // Queries User to Input Amount of USD to Convert to Foreign Currency
{
// Local Variable Declaration and Initialization
char chr_USERINPUT[80] = {'\0'};
double dbl_INPUTUSD = 0.0; // US Dollar Input Variable
// Query User for Input and Validate
while (dbl_INPUTUSD <= 0.0)
{
// Query User and Accept Input
fflush(stdin);
printf("Please Enter and Amount of US Dollars to Convert: ");
scanf("%s", chr_USERINPUT); // Input String
printf("\n\n");
printf("%s", chr_USERINPUT); // Displays String Input
// Convert String Input to Float
dbl_INPUTUSD = atof(chr_USERINPUT); // PROBLEM AREA
printf("\n\n");
printf("$f", dbl_INPUTUSD); // Displays Converted Variable
// Validate Input
if (dbl_INPUTUSD <= 0.0)
printf("\n\nYour Input is Completely Invalid!\n\n");
}
return (dbl_INPUTUSD);
}
Output is as follows for this function before the return.
Please Enter and Amount of US Dollars to Convert: 3
3 <- Input from String Displayed
$f <- PROBLEM
- RIch B
Start Free Trial