Thread: Hello, could I get a pointer for this?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Hello, could I get a pointer for this?

    Hi all

    I'm extremely new to coding. I have been working on this piece for a school assignment for a couple days now and I am stuck. Basically it is to come up with tax amounts and total sales for a fictional company. It compiles fine, but it doesn't seem to want to return valid input. No matter what amount I enter in as input, I always get "invalid input" returned. I can't figure out why because it looks right to me. Can someone point me in the right direction please?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    // Declare variables
    
    float sales = 0.0;
    float trateDelmar = .0725;
    float trateEncinitas = .0750;
    float trateLajolla = .0775;
    float staxDelmar = 0.0;
    float totamtDelmar = 0.0;
    float staxEncinitas = 0.0;
    float totamtEncinitas = 0.0;
    float staxLajolla = 0.0;
    float totamtLajolla = 0.0;
    char input[10];
    
    	// Program header
    	printf("\nTax calculation program for Kudler stores\n");
    	printf("\nEnter Purchase Amount: ");
    	
    	// Read the input and assign it to sales
    	scanf("%s", input);
    	sales = atof(input);
    	
    	if(sales <= 0.0);
    	{
    		printf("Invalid input \n");
                    return 1;
            }
            // Calculating the sales tax and total amount
            staxDelmar = trateDelmar * sales;
            totamtDelmar = staxDelmar + sales;
            staxEncinitas = trateEncinitas * sales;
            totamtEncinitas = staxEncinitas + sales;
            staxLajolla = trateLajolla * sales;
            totamtLajolla - staxLajolla + sales;
            
            // Displaying the tax rate, sales tax and total amount	
    	printf("\n        \tTax\tSales\tTotal\n");
    	printf("District\tRate\tTax\tSale Amount\n");
    	printf("------------------------------\n");
    	printf("Del Mar       \t%4.2f\t%4.2f\t%4.2f\n", trateDelmar, staxDelmar, totamtDelmar);
    	printf("Encinitas     \t%4.2f\t%4.2f\t%4.2f\n", trateEncinitas, staxEncinitas, totamtEncinitas);
    	printf("LaJolla       \t%4.2f\t%4.2f\t%4.2f\n", trateLajolla, staxLajolla, totamtLajolla);
    	
    	return 0;	
    }
    
    // End of program
    I just don't understand why this isn't working. If someone can see why, an explanation would be great. I am having a hard time understanding why this doesn't work correctly.

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    if(sales <= 0.0);

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you declare sales, you set it to 0.0. Then, you do nothing else with it until you compare sales to being <= 0.0.

    And it is.

    Todd

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Todd Burch View Post
    When you declare sales, you set it to 0.0. Then, you do nothing else with it until you compare sales to being <= 0.0.

    And it is.
    The OP does assign to it with
    Code:
          sales = atof(input);
    although I don't understand why he doesn't just read it in directly as a float with
    Code:
          scanf("%f", &sales);

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by robatino View Post
    The OP does assign to it with
    You're right. Missed that. I need to either sit closer to the monitor, put on my glasses, or bump the font size a bit!! Good catch.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    We are supposed to have it validate. So I needed a way to invalidate someone putting in a non numeric input. Was this not the right way to do this?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right here:
    Code:
    sales = atof(input);
    
    add this in:
    printf("%f", sales);
    
    and you'll see what's going on perhaps, for yourself.
    I'm no expert on validating input, but I believe the standard approach would be to use
    fgets(buffer, size of buffer, stdin);

    and then to use sscanf, etc., on buffer to "get it clean and separated.

  8. #8
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by mauhdeeb View Post
    We are supposed to have it validate. So I needed a way to invalidate someone putting in a non numeric input. Was this not the right way to do this?
    At a minumum:
    Code:
    /*
     * fcanf.c
     * get a floating point
     */
    #include <stdio.h>
    
    int main( void )
    {
        float x;
        char c;
        
       /* c is to read the ENTER key in case is a good input */
        if( scanf( "&#37;f%c", &x, &c ) != 2 )
        {
            x = 0.0; /* default value */
            while( getchar() != '\n' );
        }
        
        
        printf( "%f\n", x );
        getchar();
        return 0;    
    }
    Buffer stdin will not be clean if the right input float is entered together with more non-numerical characters before pressing the ENTER key.
    Last edited by Aia; 01-27-2008 at 08:31 PM. Reason: Clarifying

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    That is beyond the scope of this assignment, fortunately. A char should return invalid input and a number should proceed with the process. I can see it is assigning sales my input but it is running the "if" statement regardless of what I input.

    I appreciate the responses and will dig at this more considering what I have been told.

    Thank you all

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You DID remove the semi-colon at the end of this if statement, RIGHT??

    Code:
    if(sales <= 0.0);
    THAT definitely should not be there, and will cause a problem like you're having.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Oh man I been staring at this for too long, I didn't catch that. Thanks for the assist, that did it. This stuff can drive you nuts

    Thanks again!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Turn up your warnings.
    if(sales <= 0.0);
    Should easily be caught by the compiler and spew a warning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Turn up your warnings.
    if(sales <= 0.0);
    Should easily be caught by the compiler and spew a warning.
    Really? - most compilers (if not all) that I've worked with don't detect this even at the highest level of warnings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by matsp View Post
    Really? - most compilers (if not all) that I've worked with don't detect this even at the highest level of warnings.
    You've presumably not worked with gcc then? It will show this with -W or -Wextra.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have used gcc, a lot - but I also use emacs, which will indent the code appropriately, so if you construct an empty statement after if, it shows on the indentation that I've done it wrong, so I must confess I haven't seen that one [at least not often enough to remember it].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM