I was curious about a certain property of a char value as I'm pretty sure it only has enough space to contain a single character at any point of time, unless it is defined as a character array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int count = 0;
cout << "Enter characters; enter # to quit:\n";
while (ch != '#')
cout << ch;
cout << endl << count << " characters were read\n";
Okay, so from what I understand, this program should only be able to read a single character at a time yet when I type in a word, such as "see"; it processes it just fine and displays the word "see". Am I missing something here? Shouldn't a type char only be able to contain a single character at any time? Any assistance/information would be invaluable. Thanks for your time.
It 'is' only storing a single character at a time in the ch variable. When you type in 'see' it is actually running the loop 3 times (well actually 4 cuz of new line character) and putting a different value in for ch each time. You can prove this by adding another cout statement in the loop somewhere. cin.get() sees that there is still data left in the buffer after the first letter and continues reading it until it's finished.
Ah, I see. Just to double confirm, so am I right to say that when I type "see". The first cin.get(ch) will store the "s" and echo it out. After that, as there are more letters in the buffer, it detects this thus running the while loop again until the buffer is empty?