Infinite loop problem C++ for char instead of int

  • C/++/#
  • Thread starter ChrisVer
  • Start date
  • #26
34,165
5,782
Unlike Pascal or Basic, in C there's no different between char and integer.
On many systems a char is one byte and an int is four bytes. So, no difference?
Stephanus said:
char C;
cin >> c
cin would expect an integer value.
If you type 1
cin would translate it to ascii 1, not ascii 49, character '1'
That's not true. Assuming that you meant "cin >> C" on your second line, to go with how you declared C on the line above, cin is expecting a character. If you press '1' on the keyboard, the ASCII code for '1' is what is stored in C. If you look at the bit pattern it would be the bit pattern for 49, the ASCII code for the character '1', not the numeral 1.
 
  • #27
1,316
104
On many systems a char is one byte and an int is four bytes. So, no difference?

That's not true. Assuming that you meant "cin >> C" on your second line, to go with how you declared C on the line above, cin is expecting a character. If you press '1' on the keyboard, the ASCII code for '1' is what is stored in C. If you look at the bit pattern it would be the bit pattern for 49, the ASCII code for the character '1', not the numeral 1.
I mean in C or C++ we can increment char, multiply char, substract char as in integer. We can't do that in Pascal. We have to use Byte type.
But printing char in C or C++ is different than printing integer. It depends on the format you choose.
In Basic there's no Char variable. But string is close enough to char in basic.
 
  • #28
1,316
104
That's not true. Assuming that you meant "cin >> C" on your second line, to go with how you declared C on the line above, cin is expecting a character. If you press '1' on the keyboard, the ASCII code for '1' is what is stored in C. If you look at the bit pattern it would be the bit pattern for 49, the ASCII code for the character '1', not the numeral 1.
I'm sorry, I'm sorry.
I code in C, seldom code in C++, I guess I should be more careful when posting.
So for the true 1 not '1' I think we should use int
int C;
cin >> C;
I'm open to suggestions here.
 
  • #29
ChrisVer
Gold Member
3,352
452
That's why I used a string instead, because I didn't want to have the problem between whether the program expects an integer or a character.
In particular to avoid getting in an infinite loop if by any mistake the user typed "Q" instead of "1" when the program expects an integer value. Setting the input as a string can contain both chars or ints. Or another way is what was written in the link posted by wle and also used by Mark44.
 
  • #30
I think this is for the cin is a stream of data. When we sent that data to a memory address that keep for int, char, double, the compiler make the best he can. But a string is a pointer. We cannot sent a stream to a pointer. We need a buffer like char[50] to keep strings from cin.
Note that on 64bit linux cin transfers 2byte characters.
 

Related Threads on Infinite loop problem C++ for char instead of int

Replies
2
Views
2K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
2K
Replies
14
Views
826
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
1
Views
2K
Top