How do you make a program check if an input is a float or a integer?

I'm making this program for math class, it's going to check what real number class a number is in. I want the program to check if an input is an integer or a float.

Would static_class work? I searched it up and that's what I found.

I'm not sure if the program works because whenever I enter a decimal it doesn't do anything but spam "Enter a number: "

Without static_cast, it works fine, but it still doesn't check decimals. The code also crashes after I enter a negative number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
#include <cmath>


using namespace std;

int main(int argc, const char * argv[]) {
    unsigned long long x; 
    int input;
    
    cout << "Welcome to Elian's real number program! ";
    cout << "Press 1 to start the program, and 2 to exit the program." << endl;
    cin >> input;
    cin.ignore();
    while ( input == 1 ) {
        cout << "Enter a number: ";
        cin >> x;
        if ( x > 0 && (static_cast<int>(x))) { //Crashes
            cout << "The number: " << x << " is a real natural integer." << endl;
        }
        else if ( x > 0 && (static_cast<float>(x))) { //Does not work
            cout << "That number is a real irrational." << endl;
        }
        else if ( x > 0 ) { //Crashes
            cout << "The number: " << x << " is a real integer." << endl;
        }
    }
    return 0;
}

    
maybe you can try to convert int into string and do .find() to look for ".".

for conversion: http://www.cplusplus.com/reference/string/to_string/
there is a simple example.



Well first, your program will never tell you if the user enters a float because you are reading everything into an unsigned long long type. You are better off reading everything into a double, then subtract the integer part from it and if the difference is non-zero then you know the user entered an integer otherwise double

Second, an unsigned long long type can never be less than zero and this is due to the unsigned part of that type

To get the integer part of a double, you can do as the first answer suggested and read everything into a string then find the `.`.
You can also get that integer part by making use of the trun function from the math library:
http://www.cplusplus.com/reference/cmath/trunc/

Also see this, to understand the difference between the types of casts offered by c++
http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used
You also have no if statement for a negative number so the program is going to close anyway when it receives a negative number as input.
Oh god, I just noticed I didn't put the less than sign... @JB252. Thanks for finding my error in the unsigned long long thing... I thought it would just get bigger numbers .-.

Oh, and to the find thing, this is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    double x;
    int input;
    cout << "Welcome to Elian's real number program! ";
    cout << "Press 1 to start the program, and 2 to exit the program." << endl;
    cin >> input;
    cin.ignore();
    while ( input == 1 ) {
        cout << "Enter a number: ";
        cin >> x;
       if (to_string(x).find(".") == true)) {
            cout << "The number " << x << " is a real irrational." << endl;
         
        }
       
        if ( x > 0 && to_string(x).find(".")) {
            cout << "The number " << x << " is a real natural integer."  << endl;
        
        }
        if ( x < 0 && to_string(x).find(".")) {
            cout << "The number " << x << " is a real integer." << endl;
            }
        }
    }


The program works, but when I enter a decimal it says it's an irrational and a real natural integer. http://puu.sh/gKUvb.png Like that. Negative decimals also equal to real integers and not real irrationals. Numbers 1-10 say real irrational and real natural integer.

I tried doing a bunch of or's and and's and false and trues, but it still doesn't work.

I'm thinking of putting cin >> x; inside the if statements...

Thanks for telling me about the conversion and .find()!
Last edited on
Do not use to_string(). If I entered a 3 and you store it as a double, then it would be stored as 3.000... When you use to_string() on it, it will return "3.000000". So even if I give an integer, you would find a decimal and think that it's a float.

If all you're concerned about is whether or not a number is an integer or a float, you should take input as a string and then look for a '.' in it.
Topic archived. No new replies allowed.