How to use "double" ( you can use numbers like 5.4) in your programs.
Posts 1–8 of 8 · Page 1 of 1
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.
You are saving the result to a int type variable... Save it to a double/float type.
i did not understand you correctly. I tried to use double instead of int but than it gave me errors
Originally Posted by Ronon666
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.
Originally Posted by Brinuz
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+ .
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.
Dude. this thread is like OLD. I have advanced alot further than this. now i need help with other problems xD.
Originally Posted by Ronon666
Dude. this thread is like OLD. I have advanced alot further than this. now i need help with other problems xD.