check for int

hello homies do any of yall know how to check something is a int or float

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
        int n;
        int num;
        cout << "what is the number";
        cin >> num;

        int divs [10];

        for(n=0;n<10;n++)
        {
                divs[n]=num / n;
        }

        for(n=0;n>num;n++)
        {

          if (divs[n]== a interger)      
           {    
              cout << divs[n];
             }
           }
return 0;
}



this is what I have for a program that prints factors of a number out. if (divs[n] == a intiger) is obvisly not a valid condition do any of yall know a funtion to test for that? I saw someone recomend the ctype header but I dont think any of those function would check for this if so please tell me

You need to use modulo operator %. When num is divisible by n, num % n == 0.
You can check if somethign is an int or a float by seeing what you made it.

1
2
int x; //this one is an int
float y; // this one is a float 


A ha ha.

Anyway, this:

num / n;
will ALWAYS be an int, because an int divided by an int gives you an int. If you want to allow non-integer results, make one of them a float at the time of division.

Given two int values to divide, an easy way to check for an int answer is:

1
2
3
4
if (a % b == 0)
{
  // the answer is an int
}



If that's not an option, you have to decide what counts as an int.

Is this an int?

1.000000000002 ?

How about this? Is this an int?

1.000000000000000000000000000000000000000000000000000000001 ?

If you want to start looking at floating point values and deciding if they're integers, you need to understand what a floating point number is: http://floating-point-gui.de/


Last edited on
Put your a float number into a string, next just use string::find.
Last edited on
some reason it wont produce output but it compiles
i read web page on floats mos and yeah i didnt know that about floats however still dont know how to fix my problem. right now i took the first loop out and used module on num%n and i go float exception error after that changed all varibles to floats but that didnt work either.
What code are you talking about?

Example:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
   int a, b;
   std::cin >> a >> b;
   if (a % b == 0) std::cout << "a/b = integer " << a/b;
   else std::cout << "a/b = float " << float(a)/b;
   std::cin.get();
}
Last edited on
Topic archived. No new replies allowed.