We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,194 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

4
Contributors
8
Replies
16 Hours
Discussion Span
2 Years Ago
Last Updated
9
Views
Question
Answered
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: 5
Solved Threads: 0
Skill Endorsements: 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: 583
Skill Endorsements: 11

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
Skill Endorsements: 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,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

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
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15

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: 583
Skill Endorsements: 11

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
Skill Endorsements: 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
Skill Endorsements: 0
Question Answered as of 2 Years Ago by jonsca, VernonDozier and firstPerson

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: 583
Skill Endorsements: 11

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1021 seconds using 2.75MB