Hello. I need some help with variable types. I am currently an 11th grader taking my first Computer science class. I am learning C++ and I would like to know how I can make 10/3 3.33 rather than 3 in C++. For example, a member on the forums updated my code for me and made my basic calculator better but division values don't hold decimals.

``````#include <iostream>

// A more complex, yet, code effective way to program =)
// Hope this helps you understand, I'll also add some other features
// You can work out, this may help with your learning (or mess your head)!

int addFunc(int x, int y);
int  subFunc(int x, int y);
double divFunc(int x, int y);
double multFunc(int x, int y);
using namespace std;

int main()
{
int select, x, y;

cout << "Please enter your first number: ";
cin >> x;
cout << "\n Please enter your second number: ";
cin >> y;
/*
While loop: This runs a particular block of code until the comparision is true
In this case, until the user selects "5" in which time, the program
will quit =). While loops are useful because in this instance, the user
can do many calculations with the two numbers entered.
*/

while (select != 5)
{
cout << "\n Menu" << endl;
cout << "1) Addition" << endl;
cout << "2) Subtraction" << endl;
cout << "3) Division" << endl;
cout << "4) Multiply" << endl;
cout << "5) Quit" << endl;
cin >> select;

switch (select)
{
case 1:
cout << "Your result: " << addFunc(x, y);
break;

case 2:
cout << "Your result: " << subFunc(x, y);
break;

case 3:
cout << "Your result: " << divFunc(x, y);
break;

case 4:
cout << "Your result: " << multFunc(x, y);
break;
case 5:
return 0;
break;

default:
cout << "Your number was not between 1-4";
break;
}
}
return 0;
}

/*
For each of your functions, you seem to be outputting the same information
and inputting the data everytime the function is called, there is no need.
Have your functions return a value, and, in main request the input of the data.

Note: When you return data in a function, remember that the function declaration needs
to be a data type, for example, if I'm returning an integer, then it would be int.

*/
int addFunc(int x, int y)
{
return x+y;

}
int  subFunc(int x, int y)
{
return x-y;
}
double divFunc(int x, int y)
{
return x/y;
}
double multFunc(int x, int y)
{
return x*y;
}``````

int types do not support floating point decimal. try using double types, and if you desire a set precision, you can go even further and use float's.

with your calculator application, it is possible to prompt the user for a desired precision, in which case you would use float …

## All 3 Replies

int types do not support floating point decimal. try using double types, and if you desire a set precision, you can go even further and use float's.

with your calculator application, it is possible to prompt the user for a desired precision, in which case you would use float types.

Thanks, works perfectly, just replaced double divfunc(int x, int y) with double divfunc(float x, float y) and it works.

commented: wicked awesome. +10

declare

``double x, y;``

and use. To handle the precisions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.