Thread: Cannot resolve error

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Cannot resolve error

    I'm new in C programming and i have this assignment.

    Prompts the user to enter the radius of the circle. A real number
    is expected.

    (b) Prints on the screen the circumference of the circle in the
    following format:
    “The circumference of a circle with radius x ft is equal to x.xx ft.”

    (c) Prints on the screen the area of the circle in the following format:
    “The area of a circle with radius x ft is equal to x.xx sq. ft.”

    The results should be printed to a precision of 2 decimal digits.

    Code:
    #include<stdio.h>
    void main()
    {
    int r;
    int pi=3.14;
    float cir,area;
    printf("/n enter the radius of the circle");
    scanf("%d",&r);
    cir=2*pi*r;
    area=pi*r*r;
    printf("the circumference of the circle is%d", cir);
    printf("the area of the circle is%d", area);
    }
    This is my code so far. It comes out with 2 errors on the last two lines. If someone could help me resolve these and get the program running that would be great.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Part of your problem is you are not using the proper format specifiers for your printf(). The %d specifier is the specifier for a int, not a float. Also in C an int does not contain any fractional parts, so 3.14 would actually be 3. And when you are doing math using int values they will not produce fractional parts. So if your radius is 1 cir would be 2*3*1= 6. and pi would be 3. I recommend that you change all your variables to float, or better yet double.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    So if i changed all the variables to Double, how do i fix line 8 and lines 11 and 12?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Read the documentation for the functions you are using and use the proper format specifiers for the type. Here are a couple of links that may help: scanf() printf().

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Alright i have all the variables sorted out, i have no errors now. But how do i get it to display 2 decimal places, i have rounded big numbers

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Like Jim said, %d is used with integers. Use %f in the final two printf statements, since cir and area are of the type float.

    Let's talk about pi now. First of all, 3.14 is not an integer. It is a float, like cir and area. Second of all, it's a constant. You don't need to set aside 4 bytes at run-time for a value that doesn't change. It would be more efficient to use

    Code:
    #define PI 3.14
    at the top of your source file, before main() and after your #includes. This tells the compiler to replace every instance of "PI" with "3.14" before it compiles your code. Or you could use

    Code:
    const float pi = 3.14;
    which essentially does the same thing. Note the "const" identifier.

    EDIT: To print a float to only two decimal places, use %.2f instead of %f for your printf statements.
    Last edited by Babkockdood; 04-24-2012 at 02:07 PM.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Thanks everyone, i got everything resolved and done :]
    Quote Originally Posted by Babkockdood View Post
    Like Jim said, %d is used with integers. Use %f in the final two printf statements, since cir and area are of the type float.

    Let's talk about pi now. First of all, 3.14 is not an integer. It is a float, like cir and area. Second of all, it's a constant. You don't need to set aside 4 bytes at run-time for a value that doesn't change. It would be more efficient to use

    Code:
    #define PI 3.14
    at the top of your source file, before main() and after your #includes. This tells the compiler to replace every instance of "PI" with "3.14" before it compiles your code. Or you could use

    Code:
    const float pi = 3.14;
    which essentially does the same thing. Note the "const" identifier.

    EDIT: To print a float to only two decimal places, use %.2f instead of %f for your printf statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot resolve a compile error with GCC compiler in Fedora
    By anishjp in forum Linux Programming
    Replies: 4
    Last Post: 03-05-2012, 11:46 AM
  2. How to resolve this error in c++?
    By hr1212s in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2010, 02:47 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Can't resolve linker error!!!!
    By benshums in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 02:48 PM
  5. Resolve error in Windows Macro?
    By Adock in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 08:41 PM