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);
}

Recommended Answers

All 5 Replies

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).

Thanks man thats exactly what im looking for

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

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.

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.

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.