a problem with the validty of my tax program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zero9899
    New Member
    • Mar 2008
    • 1

    a problem with the validty of my tax program

    ok not sure how to accomplish my goal but's it's simple i want the program to ask if 125 was the correct amount entered but im not sure what is the simplest way to accomplish that i cant seem to get while and if to work. the program hangs on the if i know where the chack should go in the codde but i cant figre it out any suggestions would be helpful






    /code
    #include <stdio.h>
    int main (void)
    {

    float Tax[3]={7.25, 7.5, 7.75}; //this float is the number that are used for the tax wiht the differnt places

    float A = 0.0f; // this is the price of the item that will be used to find tax

    int B = 0; // this is will pick the data that will be muiltply the tax
    char ANS = ' ' ;


    printf("Enter price of item: "); // this will be disply on the screen

    scanf("%f", &A ); // this is where the price will be assigned to the a value

    printf( "You enter %.2f, correct?\n" ,A) ;

    scanf ("%s", ANS);


    printf("Stores: (1) Delmar, (2) Encintas, (3) La Jolla? "); // this denots what tax percentage will be used

    while ((B < 1) || (B > 3))
    { //this is to set the number and display the enter number
    scanf("%i", &B ); //u
    if ((B < 1) || (B > 3))
    printf (" Enter the number associated to the store: "); //this message will appear until a number will be pressed
    } // while
    printf("The sales tax is %.2f" , A*(Tax[B-1]/100)); //


    return 0; //

    } // main
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    The problem is in the line where you read a char
    [code=c]

    //scanf ("%s", ANS); change this to
    scanf ("%s", &ANS);

    [/code]

    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
      Due to the automatic rounding, these operators may report a condition as true when the numbers
      are only close in value.

      Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

      To compare two floating point numbers, you have to establish a sigma error tolerance.

      Code:
      if (fabs(i - j) < 0.000001) { ... // almost equal }

      Comment

      Working...