Question About std::cin

Here's the program. Basically it prompts the user to enter a sentence and uses std::cin to input it. It then displays the sentence as it was entered originally and also it displays it with the spaces between the words removed. For a test input I used "This is a sentence.":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using std::endl; 
using std::cout; 
using std::cin; 
using std::string;

int main()
{
   string inputString, strWithSpaces, strWithoutSpaces;
   cout << "Enter a sentence (ctrl+z when done):" << endl;
   while (cin >> inputString)
   {
      strWithoutSpaces += inputString;
      strWithSpaces += (inputString + " ");
   }

   // Display the concatenatedStrings.
   cout << strWithoutSpaces << endl;
   cout << strWithSpaces << endl;

   return 0;
}


ctrl+z should end the while loop. However the while will not end UNLESS I hit enter before the ctrl+z. If I hit the ctrl+z right after the '.' of the sentence, or after a space after the '.', the while loop keeps going. The while will only end after I enter the ctrl+z after a newline. Why is this?
Last edited on
Topic archived. No new replies allowed.