0

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

7
Contributors
16
Replies
17
Views
8 Years
Discussion Span
Last Post by DoEds
Featured Replies
  • [QUOTE=Ancient Dragon;892346]%i is the same as %d, they are interchangeable but I believe %d is more common.[/QUOTE]They are the same for [ICODE]printf[/ICODE], but they are different for [ICODE]scanf[/ICODE]. Both are used for integers, though. [ICODE]%i[/ICODE] may read the input as hexadecimal or octal as well as decimal. Read More

0

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


}

Edited by mike_2000_17: Fixed formatting

0

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.

0

%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]

0

#include <stdlib.h>

Also

2nd Printf spelled wrong? Should be lower case!

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

1

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!

Votes + Comments
good advice
1

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!

Votes + Comments
good advice -- and you are right about %i
1

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

Votes + Comments
right -- scanf() is different than printf()
0

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.

Edited by mike_2000_17: Fixed formatting

0

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

}
0

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

0

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.

0

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

0

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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.