Welcome! Log In Create A New Profile

Advanced

while (cin >> strOneWord) {}

Posted by blacksheep 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
while (cin >> strOneWord) {}
May 25, 2006 01:55PM
I keep on writing these elaborate functions for grabbing a list of strings from the console, even though the textbook shows a very nice example of how to grab a list of text words from the conole, this sample does not work 100% for me.

Refer to sample program 4.8 page 197. The program transfers control back to the console when exiting the while. Thus preventing further execution.

Any help?

My rough version of sample:

cout << "Enter Sentence: ";

string strWord,strSentence;

while(cin >> strWord) {
strSentence += strWord;
}

cout << endl << "The sentence you have entered: " << strSentence

avatar Re: while (cin >> strOneWord) {}
May 25, 2006 04:15PM
I usually use something like:


char c;
string s;

while((c = cin.get()) != 10){  // 10 is enter
  s += c;
}

cout << s << endl;

Hope this helps.

-Valkeye
Re: while (cin >> strOneWord) {}
May 31, 2006 02:34PM
Hi,

The following code:

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;

int
main(void)
{
string sentense, words;

cout << "Enter word: ";

while (cin >> words)
sentense += words;

cout << "Sentence: " << sentense << endl;

return 0;
}

Output:
Enter word: this is a test
^Z <-- CTRL+Z 'Stream Terminator'

Sentence: thisisatest
Press any key to continue

This code works fine 100%, the problem you could be experience is the termination? istream utilises CTRL+Z on a new line inorder for the stream to terminate correctly.

Hope this helps
-Seelan
Sorry, only registered users may post in this forum.

Click here to login