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.
#include <iostream>
#include <cmath>
usingnamespace std;
int main(int argc, constchar * argv[]) {
unsignedlonglong 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;
}
elseif ( x > 0 && (static_cast<float>(x))) { //Does not work
cout << "That number is a real irrational." << endl;
}
elseif ( x > 0 ) { //Crashes
cout << "The number: " << x << " is a real integer." << endl;
}
}
return 0;
}
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/
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:
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
int main(int argc, constchar * 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()!
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.