This is my first C program. I am modifying an existing program to have input and when I added two lines of code to 1) print "Enter Purchase Amount" and 2) scanf to capture the data my other printf statement received a compile error that it has the wrong # of args in function call. Now this same printf statement compiled fine before I added the other two lines. How does adding lines cause other working lines to fail to compile?

printf("\nEnter Purchase Amount: ");
scanf("%i", &iPurchase);]

Existing Code that is Failing (but worked before added code) printf("\nTax for Del Mar is %.2f", fresult1);

%i ???
do you mean %d or %u ?

Did you define iPurchase somewhere?

int iPurchase;

Yes, here is the code in its entirety. I hope you don't mind that I posted the whole thing, but it is a pretty small program.

main()
{

float foperand1 = .0725;
float foperand2 = .075;
float foperand3 = .0775;
int iPurchase = 0;
float fresult1;
fresult1=foperand1*iPurchase;

printf("\nEnter Purchase Amount: ");
scanf("%i", &iPurchase);[/COLOR]Printf("\nTax Calculator for Kudler Fine Foods");
printf("Tax for Del Mar is %.2f\n", fresult1);


}

move line 9 down to between lines 12 and 13. Programs are executed in sequence, so the calculation can not be performed until after the value of the variables are known.

%i ???
do you mean %d or %u ?

%i is the same as %d, they are interchangeable but I believe %d is more common.

[edit]printf() they are the same, but %i can not be used in scanf()[/edit]

#include <stdlib.h>

Also

2nd Printf spelled wrong? Should be lower case!

And don't forget the scanf( "%d", &iPurchase );

As a rule of thumb keep code on separate lines. There is no executable savings and concatenating the lines hides problems such as in your case!

"%i" Hmm! I'll have to look that one up. May be compiler specific!

commented: good advice +11

Oh, gotta run, but finally!

float foperand1 = 0.0725f;
float foperand2 = 0.075f;
float foperand3 = 0.0775f;

The constant float is a double unless a (f) is appended! Thus you have a compiler error of precision loss. Also ALWAYS insert a leading zero, never a (.) by itself. It can get lost!

commented: good advice -- and you are right about %i +36

%i is the same as %d, they are interchangeable but I believe %d is more common.

They are the same for printf , but they are different for scanf . Both are used for integers, though. %i may read the input as hexadecimal or octal as well as decimal.

commented: right -- scanf() is different than printf() +36

Yes, here is the code in its entirety. I hope you don't mind that I posted the whole thing, but it is a pretty small program.

main()
{

float foperand1 = .0725;
float foperand2 = .075;
float foperand3 = .0775;
int iPurchase = 0;
float fresult1;
fresult1=foperand1*iPurchase;

printf("\nEnter Purchase Amount: ");
scanf("%i", &iPurchase);Printf("\nTax Calculator for Kudler Fine Foods");
printf("Tax for Del Mar is %.2f\n", fresult1);


}

end quote.

Thanks for pointing out my error with line placement. Logic escapes me when it comes to learning a new language.

I made the adjustments recommended in most of the posts and I still receive wrong # of args on the last printf. I compiled after adding each line and it doesn't fail until the last printf. Does anyone have any more ideas on this? It is driving me crazy.

main()
{
float foperand1, foperand2, foperand3, fpurchase, fresult1;

foperand1 = .0725f;
foperand2 = .075f;
foperand3 = .0775f;
fresult1 = 0.0f;
fpurchase = 0.0f;


printf("\nEnter Purchase Amount: ");
scanf("%d", &fpurchase);

fresult1=foperand1*fpurchase;
printf("\nTax Calculator for Kudler Fine Foods");
printf("\nTax for Del Mar is %f.2\n", fresult1);

}

I don't see the reason for the wrong # of arguments but your last formatting is wrong if you're trying to set two places. The #.# must appear between the % and the f!
%.2f

change "%f.2" to "%.2f"

[edit]what ^^ said too :) [/edit]

I get that error message when I forget to put #include <stdio.h> at the top of my code. Perhaps you have forgotten it too.

NO PROBLEM WITH THE CODE POSTED BY YOU.
only 'P' is in caps in the printf function. thats it.

and if u need the resule the move the line 9 to in between 12-13 as suggested by Ancient Dragon.

#include<stdio.h>

int main()
{ float foperand1 = .0725;
float foperand2 = .075;
float foperand3 = .0775;
int iPurchase = 0;
float fresult1;
fresult1=foperand1*iPurchase;
printf("\nEnter Purchase Amount: ");
scanf("%i", &iPurchase);
printf("\nTax Calculator for Kudler Fine Foods");
printf("Tax for Del Mar is %.2f\n", fresult1);  

return 0;
}

This post should be closed because nothing to discuss on this as such.

Thanks,
Dp

The codes were okay except 2nd printf. That should be lowercase.

And also for your fresult1. It should be like this.

#include<stdio.h>

int main()
{

float foperand1 = .0725;
float foperand2 = .075;
float foperand3 = .0775;
int iPurchase = 0;
float fresult1;

printf("\nEnter Purchase Amount: ");
scanf("%i", &iPurchase);

fresult1=foperand1*iPurchase;
printf("\nTax Calculator for Kudler Fine Foods\n");
printf("Tax for Del Mar is %.2f\n", fresult1);

getchar();
getchar();
return 0;
}

And also you dont assigned extra variables when you wont use it.

float foperand2 = .075;
float foperand3 = .0775;
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.