Dynadot โ€” .com Registration $8.99

Simple C questions

Spacemail by SpaceshipSpacemail by Spaceship
Watch

aznchong91

Established Member
Impact
25
How do you find if a certain number is an integer or not? I am trying to make a simple C console application, so any help would be appreciated. Also, how do you display a variable, for example a variable called num (also in the same console application)? I really can't find a thing on the web to help me, so I have turned to the programming gurus of namepros. :D Please help!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
anyone?
 
0
•••
No one knows C?
 
0
•••
0
•••
thanks, I got displaying the variable working now, but I still can't seem to get any information on checking if a number is an integer or not... Rep added (or would add Rep, except I have to spread it... :( )!

Anyone else have any information?
 
0
•••
Well not too experienced in C myself :red:

But I think you might be looking for something like this?:
// typeid
#include <iostream>
#include <typeinfo>
using namespace std;

int main () {
int * a,b;
a=0; b=0;
if (typeid(a) != typeid(b))
{
cout << "a and b are of different types:\n";
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
}
return 0;
}
Not sure if you'll have those headers though (if that's how it works lol) :red: taken from: http://www.cplusplus.com/doc/tutorial/typecasting.html

Hope it helps,
Rhett.
 
1
•••
looks like C++ to me... :( ah well, I'll give it a try (later, when I have time), although I don't really follow the code. Rep added!
 
0
•••
anybody else have any suggestions?
 
0
•••
I'm a little confused as in theory, if the datatype of the number is 'int', then naturally you have an int :)

Where did this number come from? Is your situation something like "I have a double or a float and want to know if it has nothing after the decimal place?"

If so... I'd look at casting the number to an int then compare it to the original number.

eg.
Code:
double d = 0; // this is the number you want to check
int i = (int) d;

if ((d - i) != 0) {
     // the number had something after the decimal place
} else {
    // the number had nothing after the decimal place
}

Although that doesn't work always because of the fact that doubles and floats aren't very 'precise'.

If you have access to math.h, you could try:
Code:
double d = 0; // this is the number you want to check

if (fmod(d, 1) != 0) {
     // the number had something after the decimal place
} else {
    // the number had nothing after the decimal place
}

Well, that's what I'd look at doing, anyway...
 
1
•••
wow, gee thanks! That looks like EXACTLY what I needed! I'll give it a try! Rep added!
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back