954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File wont store floats

I have this code but the floats wont print to the file. Everything else prints and works well but the floats i see 0.000. Can someone tell me where im going wrong. I already declare them in the struct as float apaid; float tpaid;

customer customerinfo(customer info)
{
	FILE *ci;
	((ci=fopen("testing.txt","a+"))==NULL);

	printf("Customer ID: ");
	fflush(stdin);
	scanf("%d",& info.cusid);
	fprintf(ci,"\n\nCustomer ID: %8d\n",& info.cusid);

	printf("Customer first name: ");
	fflush(stdin);
	gets(info.fname);
	fprintf(ci,"Name: %s",& info.fname);

	printf("Customer last name: ");
	fflush(stdin);
	gets(info.lname);
	fprintf(ci," %s\n",& info.lname);

	printf("Email ");
	fflush(stdin);
	gets(info.email);
	fprintf(ci,"Email: %s\n",& info.email);

	printf("Phone #: ");
	gets(info.phone);
	fprintf(ci,"Phone Number: %s\n",& info.phone);
	
	printf("Amount paid to date ");
	fflush(stdin);
	scanf("%f",& info.apaid);
	fprintf(ci,"Amount paid to date: %.2f\n",& info.apaid);
	
	printf("Amount owed to date ");
	fflush(stdin);
	scanf("%f",& info.tpaid);
	fprintf(ci,"Amount owed to date: %.2f\n",& info.tpaid);
	return info;
	fclose(ci);
}
slygoth
Light Poster
43 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Line 33.

fprintf(ci,"Amount paid to date: %.2f\n",& info.apaid);


Your passing the address of info.apaid to fprintf.. It should be.

fprintf(ci,"Amount paid to date: %.2f\n", info.apaid);


Also your using fflush() incorrectly, you should never flush an input stream. If you want to flush all possible streams then call fflush(NULL).

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Thanks man thats exactly what im looking for

slygoth
Light Poster
43 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

But why didnt it work for the float but the other strings had % but it still wrote to the file anyway. Why is that?

slygoth
Light Poster
43 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 
But why didnt it work for the float but the other strings had % but it still wrote to the file anyway. Why is that?

Didn't notice the other fprintf's, you should drop the & unless your looking to print the address of a variable.

Also gets().. Never use gets always use fgets().

From my help files Never use gets(). Because it is impossible to tell without know‐
ing the data in advance how many characters gets() will read, and
because gets() will continue to store characters past the end of
the buffer, it is extremely dangerous to use. It has been used to
break computer security. Use fgets() instead.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
But why didnt it work for the float but the other strings had % but it still wrote to the file anyway. Why is that?

Because c-string's are just arrays of characters delimited with '\0', so this means passing a c-string's variable name is the same as passing the address of the variable. It works, but it looks odd and my compiler fired off a warning about a type miss match.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: