954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Variable assignment question...

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?

Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
 

For example,

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

will work

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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?

Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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

Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
 

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

Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
 

http://www.augustcouncil.com/~tgibson/tutorial/iotips.html Scroll down to "Reading in numbers directly is problematic" There's a few other sections in there that are useful to you too.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: