views:

259

answers:

4

I'm struggling with reading characters from console in c++. Here is what I tried to do:

char x;
char y; 
char z;

cout<<"Please enter your string: ";
string s;
getline(cin,s);
istringstream is(s);

is>> x >> y >> z;

The problem is if the user enter something like this "1 20 100":

x will get 1
y will get 2
z will get 0

What I want to get is x = 1; y = 20; z = 100;

Anybody has suggestions?

+3  A: 

You don't want to read characters but integers.

int x;
int y; 
int z;

cout<<"Please enter your string: ";
string s;
getline(cin,s);
istringstream is(s);

is>> x >> y >> z;
sisis
I want to read by character because i want to save memory. Character takes only 1 byte (which is good enough for my program) vs integer 4 bytes.
tsubasa
So when you want z = 100, but want z to be a character, what do you mean? Do you mean ascii character 100?
Justin Ardini
If you really want, then use `short int` (which may or may not be less than 4 bytes, depending on your compiler and architecture), but I guarantee you that if you're having trouble with something this fundamental, you are a long way away from needing to worry about optimizing to save 9 bytes of memory on the stack.
Tyler McHenry
I wouldn't worry about that at all. But if you insist, you can use a short, 2 bytes long and most likely the proper overload is defined.
sisis
lets say with z = 100, i wanna treat it as integer, not a character. By using character type instead of integer, i could save 3 bytes. Do u get my idea?
tsubasa
yes, because short int also takes 4 bytes like int (for my compiler), i can't use it.
tsubasa
@tsubasa We understand. We're saying it's not worth the hassle. This is what's called "premature optimization". You are causing yourself a an unnecessary headache and making your code confusing to save a few bytes that don't matter.
Tyler McHenry
the string s takes more than 12 bytes
sisis
@tsubasa: Why do you feel the need to save such tiny space? If any, the compiler might pad some stack variables for alignment anyway. `string` dynamically allocates, takes up a bit more space, streams in general aren't afraid to use stack variables, etc.
GMan
Thanks, i learnt a good lesson.
tsubasa
+1  A: 

You're nearly there. operator>>() is the formatted extraction operator. Change the variables from type char to type int and you're good to go.

wilhelmtell
+1  A: 

It sounds like you want to read in integers. You could do:

int x, y, z;

cout << "Please enter three integers: ";
cin >> x >> y >> z;
Justin Ardini
+1  A: 

The reason you are getting those results is that since x,y and z are chars, when you use istringstream it reads the first character into x, it skips the space and reads the character '2' into y and the next character is '0' and that goes into z.

char x, y, z;
cout << "Please enter three integers: ";
cin >> x >> y >> z;

If that doesn't work, just use ints because trying to find a workaround to use chars instead of ints to save memory is worrying about the wrong thing here.