For a program I am working on, I need a way to use an if function for a variable, if the variable is within a certain range of numbers. i.e.

if(x == 1-5) // meaning if x is from 1 to 5
//do something...
if(x == 6-10) // i.e. if x is from 6 to ten
//do something
if(x == 11-15)// ... etc
//do something

I know the code I provided is wrong and all, but it explains what I am trying to do. Does anyone know how to do it?

For example,

if(x >=1 && x <=5)

will work

Thank you, I actually figured that out about a minute after posting that hehe... but another question along those same lines if thats okay.

How can I use an if statement to decide whether the input is a character or an integer?

if(x == char)
//code here...
if(x == int)
//code here...

I know that code is totally wrong, but it gets the point across. Anyone know how to make that happen?

Thank you, I actually figured that out about a minute after posting that hehe... but another question along those same lines if thats okay.

How can I use an if statement to decide whether the input is a character or an integer?

if(x == char)
//code here...
if(x == int)
//code here...

I know that code is totally wrong, but it gets the point across. Anyone know how to make that happen?

presuming the variable x is type char, take advantage of the cctype library.

http://www.cplusplus.com/reference/clibrary/cctype/

but then this code would not make sense:

if(x == char)
//code here...
if(x == int)
//code here...

C++, unlike Java, doesn't have anything like an instanceof operator that helps you determine the type. I'm not entirely sure what you are after since you didn't post the declaration of x.

Thank you, I actually figured that out about a minute after posting that hehe... but another question along those same lines if thats okay.

How can I use an if statement to decide whether the input is a character or an integer?

if(x == char)
//code here...
if(x == int)
//code here...

I know that code is totally wrong, but it gets the point across. Anyone know how to make that happen?

First, why would you want to do that? Second you cant do that in C++, because no
matter what, you need to define the variable's type before getting an input from the user.

I think the OP is getting at being able to tell if the user has entered a number or not.
One way to do that would be to use the .fail() method of the input stream. You could also probably use stringstreams.

Im not really familiar with .fail()... where could I read up about that?

actually, VernonDozier, the link you gave me is exactly what I was looking for. Thanks a lot!

Be a part of the DaniWeb community

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