By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

cin char

P: n/a
Hi,

If you cin >x, where x if of type char, and you input a number 7, it
will read the char '7'. However, char's are really integer types, so
is it possible to set a flag so that you can input an integer that
will fit in the range of a char (say 100)? I don't think so, but just
checking.

Mar 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jraul wrote:
Hi,

If you cin >x, where x if of type char, and you input a number 7, it
will read the char '7'. However, char's are really integer types, so
is it possible to set a flag so that you can input an integer that
will fit in the range of a char (say 100)? I don't think so, but just
checking.
No. A char is a char.

--
Ian Collins.
Mar 22 '07 #2

P: n/a

"jraul" <jr*******@yahoo.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
Hi,

If you cin >x, where x if of type char, and you input a number 7,
What is really input is the character '7'. All stream operations are
done as characters.
it
will read the char '7'.
Yes.
However, char's are really integer types,
Type 'char' is one of the integer types, yes.

so
is it possible to set a flag so that you can input an integer that
will fit in the range of a char (say 100)?
You can ask that a stream convert a sequence of input digit characters
to an integer; use the proper overload:

int i;
std::cin >i;

You can determine whether this integer object's value is within
type 'char's range:

if(i >= std::numeric_limits<char>::min() &&
i <= std::numeric_limits<char>::max())
; /* in range of type 'char' */
I don't think so, but just
checking.
If you describe more specifically what you're trying to do,
perhaps we can offer better advice.

-Mike

Mar 23 '07 #3

P: n/a
On 3月23日, 上午5時02分, "jraul" <jrauli...@yahoo.comwrote:
Hi,

If you cin >x, where x if of type char, and you input a number 7, it
will read the char '7'. However, char's are really integer types, so
is it possible to set a flag so that you can input an integer that
will fit in the range of a char (say 100)? I don't think so, but just
checking.
You may try to define your own manipulator, such as :

struct Charint {
istream *fin ;
};

Charint& operator >( istream& in , Charint& foo ) {
foo.fin = &in ;
return foo ;
}

istream& operator>( Charint& foo , char & c ) {
int s ;
*(foo.fin) >s ;
c = static_cast<char>(s) ;
return *(foo.fin) ;
}
int main() {
Charint charint ;
char c ;
cin >charint >c ;
cout << static_cast<int>(c) << endl ;

cin >c ;
cout << static_cast<int>(c) << endl ;

return 0 ;
}
Mar 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.