Trying to write a program that will calculate the tax for a $125 item. It has to be able to take different tax rates from different stores. The program runs just fine, but I can't get it to spit out the tax result. line 37 is where I think the problem is???
include <stdio.h> /* is the preprocessor directive. In this case
it is including the stdio.h which is the standard input output library */
main() / the main function /
{ /the begining logical program block/
/declare and set varibles/
float d, e, l, y, n, iOperand, iOperand2, iResult;
d=.0725;e=.075;l=.0775;y=1;n='\0';iOperand=0;iOperand2=0;iResult=iOperand*iOperand2*125.00;/*Output messages and Input data*/printf("\n\tTaxCalculator\n");printf("\n\tIsitemsubjecttostatesalestax?");scanf("%f\n",&iOperand);getchar();/*getchar() is a standard I/O for get a character*/printf("\n\tStoreID?d,e,l:");scanf("%f\n",&iOperand2);getchar();printf("\n\tThetotaltaxfora$125.00itemis$%.2f\n",iResult);getchar();printf("\n\tEnd");getchar();
} / } is the end logical program block/
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
does not define an relationship, it merely evaluates teh expression on the right hand sice and assignes it to iResult. iResult will not magically re-evaluate itself when iOperand or iOperand2 changes!
This line simply needs to be moved to after the values of iOperand and iOperand2 are entered.
The code is very bizarre. You display a question that one assumes requires a Yes/No answer, but then accept a float. If the ystes enters "Yes", iOperand will be zero.
You then ask for a store ID. When a user enters d, e, or l, they are merely ASCII characters, they have no relationship to the variables d, e, or l!
You have some very fundamental misunderstanding about the way C works. The code is almost irredeemable. Since it is obviously a school assignment (no real code gets commented like that!), it is not advisable for me to post an solution. It may be useful however if you post the actual text of the assignment so you may be nudged in the right direction.
In response to Musa's comment: I think he is using getchar() to remove the newline character that remains in the buffer after the scanf() calls. It is a common kludge and severely flawed. That said, it is not necessary in this case, it is only needed to clear the input buffer when a scanf() using %s follows.
Truth is probably that the OP does not know why he has done it, and merely copied it from somewhat else - the comment merely explains what getchar() is; which is a fundamentally bad commenting style - that is what the documentation is for, comments should explain why you used it, not what it is.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Trying to write a program that will calculate the tax for a $125 item. It has to be able to take different tax rates from different stores. The program runs just fine, but I can't get it to spit out the tax result. line 37 is where I think the problem is???
include <stdio.h> /* is the preprocessor directive. In this case
it is including the stdio.h which is the standard input output library */
main() / the main function /
{ /the begining logical program block/
/declare and set varibles/
float d, e, l, y, n, iOperand, iOperand2, iResult;
} / } is the end logical program block/
This line:
iResult = iOperand * iOperand2 * 125.00;
does not define an relationship, it merely evaluates teh expression on the right hand sice and assignes it to iResult. iResult will not magically re-evaluate itself when iOperand or iOperand2 changes!
This line simply needs to be moved to after the values of iOperand and iOperand2 are entered.
The code is very bizarre. You display a question that one assumes requires a Yes/No answer, but then accept a float. If the ystes enters "Yes", iOperand will be zero.
You then ask for a store ID. When a user enters d, e, or l, they are merely ASCII characters, they have no relationship to the variables d, e, or l!
You have some very fundamental misunderstanding about the way C works. The code is almost irredeemable. Since it is obviously a school assignment (no real code gets commented like that!), it is not advisable for me to post an solution. It may be useful however if you post the actual text of the assignment so you may be nudged in the right direction.
In response to Musa's comment: I think he is using getchar() to remove the newline character that remains in the buffer after the scanf() calls. It is a common kludge and severely flawed. That said, it is not necessary in this case, it is only needed to clear the input buffer when a scanf() using %s follows.
Truth is probably that the OP does not know why he has done it, and merely copied it from somewhat else - the comment merely explains what getchar() is; which is a fundamentally bad commenting style - that is what the documentation is for, comments should explain why you used it, not what it is.
Clifford
you are calculating iResult before you get the user input of iOperand and iOperand2
are you using getchar to pause the program?