I was trying to assign values from a txt file to my program with the following code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
float c1, c2, c3, c4, c5, c6, c7, c8;
char d1[4], d2[4], d3[4], d4[4], d5[4], d6[4], d7[4], d8[4];

FILE *rate;
rate=fopen("rates.txt", "r+");

fscanf(rate, "%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f", &d1, &c1, &d2, &c2, &d3, &c3, &d4, &c4, &d5, &c5, &d6, &c6, &d7, &c7, &d8, &c8);
printf("%s\t%d\n", d1, c1);
printf("%s\t%d\n", d2, c2);
printf("%s\t%d\n", d3, c3);
printf("%s\t%d\n", d4, c4);
printf("%s\t%d\n", d5, c5);
printf("%s\t%d\n", d6, c6);
printf("%s\t%d\n", d7, c7);
printf("%s\t%d\n", d8, c8);
fclose(rate);
}

My rates.txt has the following values

USD 43.90
EUR 59.50
SGD 34.12
JPY 00.58
GBP 67.94
AUD 45.24
CAD 43.07
PHP 01.00

But when I started the program the output was different from the intended values above:

USD 43.900000
EUR 59.500000
SGD 34.120000
JPY 00.580000
GBP 67.940000
AUD 45.240000
CAD 43.070000
PHP 01.000000

How can I copy the same values to my float variables?

Just specify the precision for your float values as %.2f

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.