473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

cin char

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
3 5844
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

"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
On 3¤ë23¤é, ¤W¤È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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.