Menu

Noob needs help

2009-01-26
2012-09-26
  • Troy Riester

    Troy Riester - 2009-01-26

    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(&quot;\n\tTax Calculator\n&quot;); 
       printf(&quot;\n\tIs item subject to state sales tax? &quot;);   
       scanf(&quot;%f\n&quot;, &amp;iOperand); 
       getchar(); /*getchar() is a standard I/O for get a character*/
       printf(&quot;\n\tStore ID?  d,e,l: &quot;);
       scanf(&quot;%f\n&quot;, &amp;iOperand2);
       getchar();            
       printf(&quot;\n\tThe total tax for a $125.00 item is $%.2f\n&quot;, iResult);
       getchar(); 
       printf(&quot;\n\tEnd&quot;);
       getchar();
    

    } / } is the end logical program block/

     
    • cpns

      cpns - 2009-01-28

      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

       
    • Musa

      Musa - 2009-01-28

      you are calculating iResult before you get the user input of iOperand and iOperand2
      are you using getchar to pause the program?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.