Hi,
how could i check is a number an int or a float?

## Recommended Answers

Since you created it, shouldn't you already know?

``````int x; // this one is an int
float y; // this one is a float``````

In your case you can have a boolean which is equal to false if integer and to true if float. But cant you have only 1 function for division? Why do you need two?

answer = x/y;

That's going to return an integer anyway - int/int gives an int, no matter what the values are.

Anyway, you already decided on a way, remember?

``if ((x % y) == 0 )	//if the numbers divide evenly the answer is …``

## All 10 Replies

Since you created it, shouldn't you already know?

``````int x; // this one is an int
float y; // this one is a float``````

i'm trying to create a simple calculator. In order to output the answer to division i need determine is the result a float or an int. This is my code so far:

``````#include <iostream>
#include <sstream>
#include <fstream>
#include "conio.h"
using namespace std;
/*Write a program to simulate a simple calculator.
It should accept two number from the user along with the
required operation to be performed. Addition, Subtraction,
Division and Multiplication are the basic operations that should be implemented.
Feel free to implement the other operations (Beginner)*/

int add(int, int);
int subtract(int, int);
int divide_int(int, int);
float divide_float(int, int);
int multiply(int, int);

int main()
{
int task, x, y;
int answer_int;		//This will be printed if the answer to the divide function is a float
float answer_float;  // and this will be printed if it's an int

cout << "What function would you like to perform?" << endl;
cout << "1. Multiply" << endl;
cout << "2. Divide" << endl;
cout << "3. Add" << endl;
cout << "4. Subtract" << endl;
cin >> task;
cout << "------------------------------" << endl;

if (task == 1)
cout << "Multipy" << endl;
else if (task == 2)
cout << "Divide" << endl;
else if (task == 3)
cout << "Add" << endl;
else if (task == 4)
cout << "Subtract" << endl;
else
{
cout << "You must enter a number between 1 and 4. Press any key to terminate program";
_getch();
return 0;
}

cout << "Enter first number ";
cin >> x;
cout << "Enter second number ";
cin >> y;

switch (task)
{
case 1:
answer_int = multiply(x,y);
break;
case 2:
if ((x % y) == 0 )	//if the numbers divide evenly the answer is an integer
answer_int = divide_int(x,y);
else
answer_float = divide_float(x,y); //if not it's a float
break;
case 3:
answer_int = add (x,y);
break;
case 4:
answer_int = subtract(x,y);
break;
}
//At this point i need to determine is the returned number a float or an int
cout << "\n The answer is "; //answer_int or answer_float
cout << "\n------------------------------";

_getch();
}

int multiply(int x, int y)
{
int answer;
answer = x*y;
return answer;
}

int add(int x, int y)
{
int answer;
answer = x + y;
return answer;
}

int divide_int(int x, int y) //division function if answer is an integer
{
int answer;
answer = x/y;
return answer;
}

float divide_float(int x, int y) //division function if anser is a float
{
float answer;
answer = x/y;
return answer;
}

int subtract(int x, int y)
{
int answer;
answer = x - y;
return answer;
}``````

In your case you can have a boolean which is equal to false if integer and to true if float. But cant you have only 1 function for division? Why do you need two?

answer = x/y;

That's going to return an integer anyway - int/int gives an int, no matter what the values are.

Anyway, you already decided on a way, remember?

``````if ((x % y) == 0 )	//if the numbers divide evenly the answer is an integer
answer_int = divide_int(x,y);
else
answer_float = divide_float(x,y); //if not it's a float
break;``````

This is how you're already deciding. Why can't you do that again?

i've two because one function returns an int (when the numbers are evenly divisable) and the other returns a float (when there is a remainder to the division)

In your case you can have a boolean which is equal to false if integer and to true if float

i don't know what you mean by this?

i don't know what you mean by this?

Perhaps he means this:

``````bool isInt;
if ((x % y) == 0 )	//if the numbers divide evenly the answer is an integer
isInt = true;
answer_int = divide_int(x,y);
else
isInt = false;
answer_float = divide_float(x,y); //if not it's a float
break;``````

That's going to return an integer anyway - int/int gives an int, no matter what the values are.

oh, i didn't realise that. So how can i divide two numbers and get a float?

Anyway, you already decided on a way, remember?

oh yeah

So how can i divide two numbers and get a float?

Make one of them a float.

``float b = float(x)/y;``

great, thanks

``````// I think you should do like this
#include <stdio.h>
#include <math.h>
#include <iostream>

using namespace std;
bool functionCheckInt(float a)
{
bool result = false;
if (fmod(a * 10, 10) == 0)
{
result = true;
}
return result;
}
void main()
{
float a;
cout << "Give me a number: ";
cin >> a;
cout << endl;
if (functionCheckInt(a) == true)
{
cout << "This number is type of int" << endl;
}
else
{
cout << "This number is not type of int" << endl;
}
}``````
Be a part of the DaniWeb community

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