Thread: Beginner having difficulty spotting problem

  1. #1
    Registered User Neverender!'s Avatar
    Join Date
    Mar 2016
    Location
    Louisville, KY
    Posts
    4

    Beginner having difficulty spotting problem

    Hey guys! First post here.

    I'm a C newbie trying to put together a simple program that receives the net cost of a piece of equipment and computes the new and used sale price based on the input. I'm using NetBeans on a windows 10 machine.

    The program runs, but it seems to select random numbers for the output. Can anyone spot the inevitably newbie error I'm making in my code?

    Thanks!

    Code:
    #include <stdio.h>
    
    
     main () 
    {
        double net,usedprice,newprice;
        const double markup = 0.18;
        const double taxrate = 0.06;
        const double useddisc = 0.50;
    
    
        printf("Please enter Vittitow's cost:\n");
        scanf("%d", &net);
        newprice = ((net * markup)+ net) + (net * taxrate);
        usedprice = (((net * markup) + net) + net * taxrate) * usedprice;
        printf("New Price: $%.1f\nUsed Price: $%.1f\n", newprice, usedprice);
        return 0;
        
    }

  2. #2
    Registered User Neverender!'s Avatar
    Join Date
    Mar 2016
    Location
    Louisville, KY
    Posts
    4
    I just noticed one problem—in my usedprice math (line 15), I multiplied the whole thing by itself. The last item in line 15 should be useddisc instead of usedprice.

    Still, even with that fixed, with an input of 2500, my output looks like:

    New Price: $0.0
    Used Price: $0.0

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should find and read some documentation for the scanf() function. Using the incorrect format specifier for the type produces undefined behavior. The "%d" format specifier is not the correct specifier for double. Your compiler, if properly configured, should warn you about this kind of issue.

    Jim

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,115
    First of all, turn on and turn up your compiler warning level to the highest setting.

    Without using command line arguments in main(), it should be defined as:

    Code:
    int main(void)
    {
        /* ... */
    }
    You should always check the return value to scanf() to insure you received data.

    "net" is defined as a double, but you are using a "%d" integer format specifier.

    Code:
    usedprice = (((net * markup) + net) + net * taxrate) * usedprice;
    You are multipling by "usedprice", whish is uninitialized then assigning back to "usedprice"

    "useddisc" instead???

  5. #5
    Registered User Neverender!'s Avatar
    Join Date
    Mar 2016
    Location
    Louisville, KY
    Posts
    4
    Thanks for the responses. Rstanley, I noticed the multiplying by "usedprice" error and fixed that, thanks. I'm trying to find the proper specifier for a double. Is it "%.1f", or does double have its own specifier?

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,115
    Quote Originally Posted by Neverender! View Post
    Thanks for the responses. Rstanley, I noticed the multiplying by "usedprice" error and fixed that, thanks. I'm trying to find the proper specifier for a double. Is it "%.1f", or does double have its own specifier?
    In scanf(), "%f" for a float, and "%lf" (ell-f) for a double.

    Your book on the C programming Language, that you should have, should give you the correct format specifiers for all the data types.

    Since you are on a Windows computer, you could also check out the 'man' page here for scanf().

  7. #7
    Registered User Neverender!'s Avatar
    Join Date
    Mar 2016
    Location
    Louisville, KY
    Posts
    4
    Thanks so much! Fixing that made it run perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bulls and Cows Program - Spotting the Problem
    By YannB in forum C Programming
    Replies: 8
    Last Post: 12-19-2013, 10:37 AM
  2. Spotting the error...
    By Enegis in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2008, 05:51 AM
  3. got difficulty on this
    By gtr_s15 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 09:37 AM
  4. winHTTP : spotting a 404 (and other server errors)
    By reanimated in forum Windows Programming
    Replies: 1
    Last Post: 05-28-2004, 05:58 PM
  5. having difficulty with this problem
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-28-2002, 11:09 PM