cin >> nuances

I'm looking at a program written in a professionally published book:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	char ch;
	int count = 0;
	cout << "enter characters; enter # to quit:\n";
	cin >> ch;

	while (ch != '#')
	{
		cout << ch;
		count++;
		cin >> ch;
	}

	cout << endl << count << " characters read\n";

	return 0;
}


My question is: If you put in the sample input (after being prompted to enter characters): This is the song that never ends it goes on and on my friends

cin is reading that into a character, right? But a character can only hold 1 character at a time out of that input I just put in.

-- So where are the other characters going?
-- Are they being put into unallocated space in memory?
-- Or are they sitting in the input stream for processing INTO the program?

I guess I'm just failing to see
-- at what point does the input actually reach the program?
-- and why can I read a sentence into a char using cin?
-- Wouldn't it be more appropriate to use a string?

Sorry for all the questions; it just seems like an odd input machanism.
at what point does the input actually reach the program?
The standard input waits in a buffer until something evokes a read from it (I'm sure there's a more technical explanation). cin will (should) only read enough input as is necessary for what it's reading into.
why can I read a sentence into a char using cin?
Because (as you pointed out) a char can only hold one character.
Wouldn't it be more appropriate to use a string?
Yes.

Edit: Aww now I feel guilty --v
Last edited on
The cin >> ch; line reads a single character.
The rest of the characters that you entered sit inside the input buffer until they're read.
(Internally, there's a std::streambuf object that handles these things: http://www.cplusplus.com/reference/streambuf/streambuf/ )

Note that cin overwrites the previous value of ch every time it grabs a character from the input buffer, so it's not actually reading the whole sentence into one char all at once.

And yeah, a std::string would be easier here:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    std::cout << "Enter characters; enter # to quit:\n";
    std::string str;
    std::getline(std::cin, str, '#');
    
    std::cout << '\n' << str.size() << " characters read.\n";
}
Note that the output of this program may be a bit different from the program you posted above, because cin >> something ignores leading whitespace, while std::getline just dumps the whole thing (whitespace and all) into one string.

EDIT: Aww, someone beat me to it.
Last edited on
Both of your answers are very unique but congruent with the same idea so I appreciate both of them equally; you both added something I didn't know so thanks for your answers!

When inputting to a char with cin >> ch, if you input:

This is the song that never ends.

Only the "T" (in "This") is being read into the "ch" (char variable) and the rest (his is the song that never ends) remains in the input buffer, until extracted by some other fetching method.

Thanks very much for your answers,

Aaron
Topic archived. No new replies allowed.