Results 1 to 8 of 8
  1. #1
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored

    How to use "double"? ( you can use numbers like 5.4) in your programs?

    Hey, i know that you can use double in java but what about an double or somthin like that in c++.
    Im currently making a converter(If you can call it that) and since it is my 2nd program i find it pretty awesome.

    So its like 25mm2 = 0cm2 but i want it to be

    25mm2 = 0.25cm2

    so what do i have to type to get that 0.25cm as an answer.

    Code:
     #include <iostream>
    int main()
    {
    using namespace std; 
    int num, num2, num3 , num4, num5, mynumber;
    cout << "Enter the number you want to square: (im going to think that its a mm)";
    cin >> num;
    cout << "Really? O.o you dont have the brain to square it yourself? fine than... press any key to see the answer." << endl;
    system("pause");
    num2 = num * num;
    cout << " Here you go you lazy monkey: " << endl;
    cout << num;
    cout << " mm = ";
    cout << num2;
    cout << "mm2" << endl;
    cout << num2;
    cout << " mm2 = ";
    num3 = num2 / 100;
    cout << num3;
    cout << "cm2" << endl;
    cout << num3;
    cout << " cm2 = ";
    num4 = num3 / 100;
    cout << num4; 
    cout << "dm2" << endl;
    cout << num4;
    cout << " dm = ";
    num5 = num4 / 100;
    cout << num5;
    cout << "m2" << endl;
    cout << num5;
    cout << " m2 = ";
    mynumber = num5 / 100;
    cout << mynumber;
    cout << "km2" << endl;
    system("pause");
    return 0;
    }
    Thats my code, how can i make it so it gives me 0.25 instead of 0.
    Last edited by Ronon666; 06-14-2011 at 07:54 AM.



    Dont be a TOOL say Obamas cool.

  2. #2
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    You are saving the result to a int type variable... Save it to a double/float type.

    Check this:
    https://www.exforsys.com/tutorials/c-...ata-types.html
    Last edited by 'Bruno; 06-14-2011 at 08:57 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  3. The Following User Says Thank You to 'Bruno For This Useful Post:

    Hell_Demon (06-18-2011)

  4. #3
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored
    i did not understand you correctly. I tried to use double instead of int but than it gave me errors

  5. #4
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Quote Originally Posted by Ronon666 View Post
    i did not understand you correctly. I tried to use double instead of int but than it gave me errors
    Instead of

    Code:
    int num, num2, num3 , num4, num5, mynumber;
    use

    Code:
    double num, num2, num3 , num4, num5, mynumber;
    and your code should work just fine.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  6. #5
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored
    Quote Originally Posted by Brinuz View Post
    Instead of

    Code:
    int num, num2, num3 , num4, num5, mynumber;
    use

    Code:
    double num, num2, num3 , num4, num5, mynumber;
    and your code should work just fine.
    Okey thx, i tried to use it before myself but it gave me an error. Gonna see if it works now.

    O.o im confused, it didnt work earlier but it did now, well i must have made a typo somewhere. mhm there are still some fails in my program, the maximum length or W/E you call that in english can be max 999mm (Gonna make it the other way so max will be 999km so its something thats worth posting)

    O.o this program is a failure unless theres a way to show the correct number, (If the number is too big it shows e+ at the middle of the number deleteing some numbers from the answer)
    Is there a way? since currently if i would do it with mm at the starting number the max you would be able to enter is 999 but even if i type 0.1 while km is the starting one it still shows e+ .
    Last edited by Ronon666; 06-14-2011 at 09:29 AM.



    Dont be a TOOL say Obamas cool.

  7. #6
    Pxpc2's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    231
    Reputation
    6
    Thanks
    11
    My Mood
    Angelic
    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
    double f;
         double m;
    
               cout << "Enter the length in feet:  ";
               cin >> f; // thats reading the number of f (feet)
    
               m = f / 3.28; // "converting" (soz my english is bad haha)
               cout << f << " feet is " << m << "meters.";
    
               return 0;
    }
    That would create a program that could convert feet into meters, thats pretty easy to create, and a good example of Double, if you can see I used Double instead of int for ammount of feet like 4.9, 4.5 feet etc...

    So thats how you use double, and already gave a good example of a simple conversation for you so this could help in your program..

    Sorry for syntax errors if you get cause im not coding in c++ anymore, im using Java.

    Also sory for spelling errors cause my english is not sooooo good.

  8. #7
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored
    Dude. this thread is like OLD. I have advanced alot further than this. now i need help with other problems xD.



    Dont be a TOOL say Obamas cool.

  9. #8
    Pxpc2's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    231
    Reputation
    6
    Thanks
    11
    My Mood
    Angelic
    Quote Originally Posted by Ronon666 View Post
    Dude. this thread is like OLD. I have advanced alot further than this. now i need help with other problems xD.
    Im sorry and no need to be rude.

    You should have alreayd requested a close.