char variables

consider the following program segment:
char a, b;
cin>>a; cin>>b;
cout<<a<<" "<<b;

if we input:
2(enter)
3(enter)
2 gets stored in a and 3 gets stored in b as expected
but if we input:
23(enter)
the same thing happens. can anyone tell me why this happens?
Is there any way i can stop this from happening?
It's because char variable can store only one character so when you type 23, 2 stored in a & 3 stored in b.you can't store 23 in char variable
Last edited on
so is there anyway to stop the user from inputting both the values in one go?
what i mean is can we make the computer ignore 3 and store 2 in a?
As you say

char a, b;
cin>>a; cin>>b;
cout<<a<<" "<<b;

if we input:
2(enter)
3(enter)
2 gets stored in a and 3 gets stored in b as expected
but if we input:
23(enter)
the same thing happens. can anyone tell me why this happens?


computer is stroing 2 in a,or i misunderstand you?
when i said the same thing happens, i meant that 2 gets stored in a and 3 gets stored in b but
i don't want to allow the user to input both values in one go.
i want each input to be separated by (enter). is there a way to do so?
Topic archived. No new replies allowed.