Arrrrrggghhh

This is a discussion on Arrrrrggghhh within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main() { int mass; cout<<"Enter the mass in kg: "; cin>>mass; int C=300000000; double energy=double(mass*(C*C)); cout<<"This ...

  1. #1
    Unregistered
    Guest

    Arrrrrggghhh

    Code:
    #include <iostream.h>
    
    int main()
    
    {
    	int mass;
    	cout<<"Enter the mass in kg: ";
    	cin>>mass;
    	int C=300000000;
    	double energy=double(mass*(C*C));
    	cout<<"This mass would produce "<<energy<<" Joules of energy."<<endl;
    	cout<<"It could power "<<(energy/360000)<<" 100-watt light bulbs for an hour."<<endl;
    	return (0);
    }
    enter mass 0.01
    should produce 9e+14
    light 2.5e+09 light bulbs

    so why the hell do i get 0?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    why is there a 'double' in front of (mass*......?
    I Love Jesus

  3. #3
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    thats a big number, maybe TOO big for your system?

    its happened before...if you use a too big of a number it goes to the max which is like 2729398012 or something lol
    Paro

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    As I recall, and int can only contain values ranging from -32000 to +32000 (or thereabouts). This statement:

    int C=300000000;

    is assigning a very large number to a small area.

    I don't have my book on me, but to point you in the right direction... look up the various data types, and how much data they can hold.

    You might looking at long int, or maybe unsigned long int.

  5. #5
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Did anyone notice the fact that mass is an int?
    Integer types truncate any decimal part of the number so
    0.01 = 0 as an int.
    You might start by using a floating point variable then use an unsigned long int to cover the large numbers.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  6. #6
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    taylorguitarman is god, all bow down... *bows*

    lol, i didnt even notice that, i just assumed his number was too big but then again, im a big fat hairy jelatenous noob
    Paro

  7. #7
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Sorry, not a god; it's just easier to find errors in someone else's code.

  8. #8
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Talking

    **searches for a new god**

    pop quiz:: how many bytes in 64 bits?
    Paro

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    512
    but and the regular int (32 bit) range is -2,147,483,648 to 2,147, 483, 647 .
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    sorry, I mis-read your post Paro, The correct answere is 8 not 512. 512 is the number of bits in 64 bytes 8 is the number of bytes in 64 bits.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  11. #11
    Unregistered
    Guest
    8 but...
    I think you meant how many bits in 64 bytes.

  12. #12
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    no i meant what i said and i fooled you all!!!

    NOW WHO IS GOD!!! jk
    Paro

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    If its not 512 and its not 8 what is it?
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  14. #14
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    it is 8, i didnt say it wasnt either of them...i think its 8 anyway...

    god i need a new god

    **continues search for a god**
    Paro

  15. #15
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    howbout me.....oh me me me try me
    can i be your god?
    anyway, with any variable type why am I always getting 0?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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