Square and floats please read

I made a program which is doing something but that is not important imoportant is that it is not "couting" for an example 46.00 it is 46, or not 50.00 it is 50 i want to make it work sorry for bad english but I think that u understood :)
Please help, and also how do I make a square of number 8 for an example it will return 64? :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

main()
{
      int l,h,s,povrsina,ukp;
      int endic;
      cin >> l;
      cin >> h;
      cin >> s;
      povrsina = ((l/s)*s) * ((h/s)*s);
      ukp = l*h;
      endic = (100 - 100*(float)povrsina/ukp);
      cout << endic;
      system("PAUSE");
      return 0;
}
Hey man, the first important thing to remember about C / C++ is that Data Types are important. You are currently trying to print out an integer (a whole number) so try changing this line of code:
int endic;
to this:
double endic;
you should now see decimal results. As to why you would want to display 56.00 instead of 56 I am unsure but changing the DT should work.

As for squaring a number, you can do this by multiplying the number by itself or using the pow() function like so:
cout << 8*8 << endl; cout << math::pow(8,2) << endl;

Best of luck,
Garry++
Last edited on
Hey garry thanks for answer but still it is 46 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

main()
{
      int l,h,s,povrsina,ukp;
      double endic;
      cin >> l;
      cin >> h;
      cin >> s;
      povrsina = ((l/s)*s) * ((h/s)*s);
      ukp = l*h;
      endic = (float)(100 - 100*(float)povrsina/ukp);//i think that this line is a problem maybe
      cout << endic;
      system("PAUSE");
      return 0;
}
Try using the iomanip lib.
http://www.cplusplus.com/reference/iomanip/?kw=iomanip

#include <iomanip>

Then do something like this:

cout << fixed << showpoint << setprecision(2) << endic;

Hope it helps :)
Topic archived. No new replies allowed.