-1

I have been watching youtube and different guides on internet and thought it would be fun to make a calculator because i have seen many do it, Here is what i have started. It works fine but i want it to be able to show decimals too. All answers are appreciated. (I have a bunch of #Include but ignore those)

#include <iostream>
#include <limits>
#include <cstdio>
#include <tchar.h>
#include <conio.h>

using namespace std;

int main()

{

  std::cout << "My first caclulator\nPlease enter your first number: ";
  int x, y;
  std::cin >> x;
  std::cout << "Please enter the other number: ";
  std::cin >> y;
  int w = x*y;
  int c = x + y;
  int v = x - y;
  int q = x / y;
  std::cout << "\nNumbers multiplied: " << w << endl;
  std::cout << "\nNumbers added together: " << c << endl;
  std::cout << "\nNumbers subtracted: " << v << endl;
  std::cout << "\nNumbers divided: " << q << endl;
  _tprintf(_T("Press any key to exit "));
  while (_kbhit() ) _gettch();

    _gettch();

    return 0;
}
user0042
  • 7,917
  • 3
  • 24
  • 39

2 Answers2

0

All your calculations are done with integer mathematics, therefore there are no decimals involved (all values are truncated). You can alter the code to use double but then you will end up with a large number of decimal places, so best to round using setprecision, for example:

double w = x*y;
double c = x + y;
double v = x - y;
double q = x / y;
std::cout << "\nNumbers multiplied: " << setprecision(2) << w << endl;
std::cout << "\nNumbers divided: " << setprecision(2) << q << endl;
Neil
  • 11,059
  • 3
  • 31
  • 56
  • `setprecision` would need `std::` prefix (or a `using` statement) and an additional `#include`. http://en.cppreference.com/w/cpp/io/manip/setprecision – Melebius Sep 15 '17 at 13:45
0

If you want to show the decimals, you have to use another data type.
Try something like double or float.

Example:

 double w = x*y;
 double c = x + y;
 double v = x - y;
 double q = x / y;

This should work fine.
If you need more informations about other data types check this out: link

xMutzelx
  • 566
  • 8
  • 22