Beginner: What is wrong with my code? It seems fine!

This is a discussion on Beginner: What is wrong with my code? It seems fine! within the C Programming forums, part of the General Programming Boards category; Hi there, This is my first post. I am new to prgramming and the C language, and was following instructions ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Thumbs down Beginner: What is wrong with my code? It seems fine!

    Hi there,
    This is my first post.
    I am new to prgramming and the C language, and was following instructions from a book to produce a simple execution. Unfortunately, it does not give me clean results when I try and compile it (with Xcode), so I was wondering if anybody here could help me solve it?

    It is basic, so please do not get too in depth about it, as I only briefly understand C. Thanks.
    Code:
    /* Code to calculate earnings using to variables*/
    
    #include <stdio.h>
    
    int main(void) {
    	
    	int age; /* User's age in years*/
    	float hourly_wage; /* User's hourly wage in pounds. */
    	
    	/* Set the values*/
    	
    	age=40;
    	hourly_wage= 6.50;
    	
    	
    	/* Print the variable values*/
    	
    Printf ("You are %d years old.\n", age);
    Printf ("Your hourly wage is %0.2f. This translates into %0.2f per year.\n", hourly_wage, hourly_wage*12*5*52);
    	
    	
    	
    	return 0;
    	
    }
    This is the end of my code.


    Thanks,

    Shiv

    P.S: This is the error I was getting:

    "_Printf", referenced from:
    Last edited by Salem; 08-02-2009 at 01:46 AM. Reason: Added [code][/code] tags - LEARN TO DO IT YOURSELF

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    There is no "Printf" in C, but there is "printf". C is case sensitive.
    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.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes,also please use code tags! << !! Posting Code? Read this First !! >>
    Spidey out!

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Ahhh...thanks so much mate.

    Best,
    Shiv

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Oh and just in case you weren't aware, you can use "//" for single line comments, i.e:

    // Comment

    /*
    This is for multiple
    line comments
    Although it can be used for single as well.
    */
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    // only exists in C99, though, but many compilers support it as a C90 extension.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Cheers, I didn't know that. I'm currently learning basic-C from a book I got out of the local library.

    Best,
    Shiv

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    You have to check out Deitel's book, there are lot of good exercises to fix sintax doubts such as the "case sensitive" issue of C.

    Amazon.com: C How to Program Introducing C++ and Java (9780131426443): Harvey M. Deitel, Paul J. Deitel: Books

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also, using floats for money is a bad idea! Money is *not* floating point; it's fixed precision.

    Try this:
    Code:
    #include <stdio.h>
    
    int main() {
    	float x=0.0f, i;
    	for (i=0.0f; i<20; i+=0.1f) {
    		printf("%f\n",x+i);
    	}
    	return 0;
    }
    Depending on whether you system is 32 or 64 bit, at some point you will see the expected (an increase of 0.1 at each iteration) become the unexpected:

    2.500000
    2.600000
    2.700000
    2.799999
    2.899999
    2.999999
    3.099999
    3.199999


    This has to do with the nature of floating point numbers. You may think this is okay, since you are going to round it anyway, but actually the opposite it true: if you do enough arithmetic with floats like this, you can end up losing pennies.

    When dealing with money, always use an int value, the number of cents, and convert it using division (and modulus for the remainder) for output:
    Code:
    int val = 532;     /* number of cents */
    printf("$%d.%2d\n",val/100,val%100);
    Last edited by MK27; 08-03-2009 at 03:02 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by MK27 View Post
    Also, using floats for money is a bad idea! Money is *not* floating point; it's fixed precision.
    I disagree. When calculating loans, interest, payment amount, or pension payouts, etc., money amounts are treated as high precision floating point. If you had to force each intermediate calculation to the nearest penny you'd end up with inaccurate results. Instead, rounding occurs at the very end when showing the amounts to be paid or paid out.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by nonoob View Post
    I disagree. When calculating loans, interest, payment amount, or pension payouts, etc., money amounts are treated as high precision floating point. If you had to force each intermediate calculation to the nearest penny you'd end up with inaccurate results. Instead, rounding occurs at the very end when showing the amounts to be paid or paid out.
    In this case, money is still stored as an integer value. It's just the results of calculations that are floating point variables.

    In other words, you can have something like:
    Code:
    unsigned int accrue_interest(unsigned int initial_amount)
    {
        // calculate interest and return the result
    }
    The input and output are integers, but the calculations will involve some floating point arithmetic and rounding. The reasoning for keeping it integer based is so that when you have something like:
    Code:
    unsigned int make_deposit(unsigned int balance, unsigned int deposit)
    {
        return balance + deposit;  // Won't give you any precision issues due to using integer arithmetic.
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  5. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21