1,105,592 Community Members

Variable assignment question...

Member Avatar
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

For example,

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

will work

Member Avatar
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
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.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
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?

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.

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

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.

Member Avatar
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Rickay
Junior Poster in Training
55 posts since Jul 2010
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 3 Years Ago by jonsca, VernonDozier and firstPerson
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: