Thread: operators

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    28

    operators

    i am a beginner in programming and i have a doubt:
    what is wrong with the following code? the result is always 0...

    Code:
    #include <stdio.h>
    
    float pi;
    float raio;
    float volume;
    char line[100];
    
    int main() {
    	pi = 3.14159265;
    	printf("qual o raio da esfera? ");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%f", &raio);
    	volume = (3/4) * pi * (raio*raio*raio); /* 0.75 instead of 3/4 does the job... */
    	printf("volume e %f\n", volume);
    	return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As it is integer division, 3/4 = 0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by laserlight View Post
    As it is integer division, 3/4 = 0
    thanks for the reply.
    what do i need to change to be able to use "3/4" then?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    3.0/4.0 or just 0.75 instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by hk_mp5kpdw View Post
    3.0/4.0 or just 0.75 instead.
    I can believe that i made this mistake...
    Thanks for the quick answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM