I am having a problem converting string to a double. Please help me out.
Here's my code:
char Price[100];
double newPrice;
printf("\nPlease enter the price:");
fgets(Price,100,stdin);
newPrice = atof (Price);
printf("\nPrice of item is %f",newPrice);
It gives diff output while running with diff files.
file1 (method 1):
char Price[100];
double newPrice;
int main()
{
myval();
return 0;
}
myval()
{
printf("\nPlease enter the price:");
fgets(Price, 100, stdin);
newPrice = atof(Price);
printf("\nPrice of item is %f", newPrice);
}
file2 (method 2)
#define MAX 50
char oldPrice[MAX];
double newPrice;
int main()
{
UserInput();
}
int UserInput()
{
printf("\nPlease enter the price:");
fgets(oldPrice, MAX, stdin);
newPrice = atof(oldPrice);
printf("\nPrice of item is %f", newPrice);
return 0;
}
The above two methods were compiled using tcc(tiny Compiler). Both methods are the same , but i get a diff output for these 2. output 1:
D:\>new.exe
Please enter the price:12.3
Price of item is 12.300000
Output 2:
D:\>t.exe
Please enter the price:12.3
Price of item is 7735248.000000