How to read cin into a vector
I need to allow the user to enter a writing sample in the console or as a file and have my program split that input into a word vector (one word per item of vector). This is my current code:
while(cin >> inputString) {
wordVector.push_back(inputString);
}
The trouble is, when I run this, it works fine until it reaches the end of the user's input. Then it seems to just endlessly loop.
inputString is type string.
wordVector is type string.
This is the full code: (the broken code is at the bottom)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Debug message flag
const bool DEBUG = false;
// Prototypes
void splitToVectors(vector<string>&,vector<string>&,vector<int>&,int &);
double avSentLength(const vector<string>);
double avWordSyl(const vector<string>,const vector<char>);
double percentSentLong(const vector<int>,int);
int numSyllables(const vector<char>);
void nextScreen(int);
int main() {
// Initialize variables and vectors
bool validate;
int characters,words,sentences = 0,syllables;
string file;
string inputString;
char inputChar;
int input;
vector<string> wordVector;
vector<char> charVector;
vector<string> sentenceVector;
vector<int> numWordsInSent;
// Get writing sample
do {
// Request preferred location
validate = true;
cout << "Would you like to:" << endl;
cout << " 1. Enter the writing sample in the console" << endl;
cout << " 2. Read from a file" << endl << " > ";
// Validate
if(!(cin >> input)) { // This error checking condition functions as the cin
validate = false;
cin.clear();
cin.ignore(100, 'n');
}
if((input < 1) || (input > 2)) {
validate = false;
}
} while(!validate);
// Transfer selected source to wordVector
if(input == 1) {
// Request sample
cout << "Please enter the writing sample below:" << endl << endl;
// Input sample
while(cin >> inputString) {
wordVector.push_back(inputString);
}
}
}
c++ vector cin
|
show 3 more comments
I need to allow the user to enter a writing sample in the console or as a file and have my program split that input into a word vector (one word per item of vector). This is my current code:
while(cin >> inputString) {
wordVector.push_back(inputString);
}
The trouble is, when I run this, it works fine until it reaches the end of the user's input. Then it seems to just endlessly loop.
inputString is type string.
wordVector is type string.
This is the full code: (the broken code is at the bottom)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Debug message flag
const bool DEBUG = false;
// Prototypes
void splitToVectors(vector<string>&,vector<string>&,vector<int>&,int &);
double avSentLength(const vector<string>);
double avWordSyl(const vector<string>,const vector<char>);
double percentSentLong(const vector<int>,int);
int numSyllables(const vector<char>);
void nextScreen(int);
int main() {
// Initialize variables and vectors
bool validate;
int characters,words,sentences = 0,syllables;
string file;
string inputString;
char inputChar;
int input;
vector<string> wordVector;
vector<char> charVector;
vector<string> sentenceVector;
vector<int> numWordsInSent;
// Get writing sample
do {
// Request preferred location
validate = true;
cout << "Would you like to:" << endl;
cout << " 1. Enter the writing sample in the console" << endl;
cout << " 2. Read from a file" << endl << " > ";
// Validate
if(!(cin >> input)) { // This error checking condition functions as the cin
validate = false;
cin.clear();
cin.ignore(100, 'n');
}
if((input < 1) || (input > 2)) {
validate = false;
}
} while(!validate);
// Transfer selected source to wordVector
if(input == 1) {
// Request sample
cout << "Please enter the writing sample below:" << endl << endl;
// Input sample
while(cin >> inputString) {
wordVector.push_back(inputString);
}
}
}
c++ vector cin
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
did you hitCtrl + D
orCtrl + Z
? Did any of those terminate the loop?
– TuanDT
Nov 26 '18 at 1:26
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10
|
show 3 more comments
I need to allow the user to enter a writing sample in the console or as a file and have my program split that input into a word vector (one word per item of vector). This is my current code:
while(cin >> inputString) {
wordVector.push_back(inputString);
}
The trouble is, when I run this, it works fine until it reaches the end of the user's input. Then it seems to just endlessly loop.
inputString is type string.
wordVector is type string.
This is the full code: (the broken code is at the bottom)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Debug message flag
const bool DEBUG = false;
// Prototypes
void splitToVectors(vector<string>&,vector<string>&,vector<int>&,int &);
double avSentLength(const vector<string>);
double avWordSyl(const vector<string>,const vector<char>);
double percentSentLong(const vector<int>,int);
int numSyllables(const vector<char>);
void nextScreen(int);
int main() {
// Initialize variables and vectors
bool validate;
int characters,words,sentences = 0,syllables;
string file;
string inputString;
char inputChar;
int input;
vector<string> wordVector;
vector<char> charVector;
vector<string> sentenceVector;
vector<int> numWordsInSent;
// Get writing sample
do {
// Request preferred location
validate = true;
cout << "Would you like to:" << endl;
cout << " 1. Enter the writing sample in the console" << endl;
cout << " 2. Read from a file" << endl << " > ";
// Validate
if(!(cin >> input)) { // This error checking condition functions as the cin
validate = false;
cin.clear();
cin.ignore(100, 'n');
}
if((input < 1) || (input > 2)) {
validate = false;
}
} while(!validate);
// Transfer selected source to wordVector
if(input == 1) {
// Request sample
cout << "Please enter the writing sample below:" << endl << endl;
// Input sample
while(cin >> inputString) {
wordVector.push_back(inputString);
}
}
}
c++ vector cin
I need to allow the user to enter a writing sample in the console or as a file and have my program split that input into a word vector (one word per item of vector). This is my current code:
while(cin >> inputString) {
wordVector.push_back(inputString);
}
The trouble is, when I run this, it works fine until it reaches the end of the user's input. Then it seems to just endlessly loop.
inputString is type string.
wordVector is type string.
This is the full code: (the broken code is at the bottom)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Debug message flag
const bool DEBUG = false;
// Prototypes
void splitToVectors(vector<string>&,vector<string>&,vector<int>&,int &);
double avSentLength(const vector<string>);
double avWordSyl(const vector<string>,const vector<char>);
double percentSentLong(const vector<int>,int);
int numSyllables(const vector<char>);
void nextScreen(int);
int main() {
// Initialize variables and vectors
bool validate;
int characters,words,sentences = 0,syllables;
string file;
string inputString;
char inputChar;
int input;
vector<string> wordVector;
vector<char> charVector;
vector<string> sentenceVector;
vector<int> numWordsInSent;
// Get writing sample
do {
// Request preferred location
validate = true;
cout << "Would you like to:" << endl;
cout << " 1. Enter the writing sample in the console" << endl;
cout << " 2. Read from a file" << endl << " > ";
// Validate
if(!(cin >> input)) { // This error checking condition functions as the cin
validate = false;
cin.clear();
cin.ignore(100, 'n');
}
if((input < 1) || (input > 2)) {
validate = false;
}
} while(!validate);
// Transfer selected source to wordVector
if(input == 1) {
// Request sample
cout << "Please enter the writing sample below:" << endl << endl;
// Input sample
while(cin >> inputString) {
wordVector.push_back(inputString);
}
}
}
c++ vector cin
c++ vector cin
asked Nov 26 '18 at 1:19
Joshua MinettJoshua Minett
233
233
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
did you hitCtrl + D
orCtrl + Z
? Did any of those terminate the loop?
– TuanDT
Nov 26 '18 at 1:26
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10
|
show 3 more comments
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
did you hitCtrl + D
orCtrl + Z
? Did any of those terminate the loop?
– TuanDT
Nov 26 '18 at 1:26
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
did you hit
Ctrl + D
or Ctrl + Z
? Did any of those terminate the loop?– TuanDT
Nov 26 '18 at 1:26
did you hit
Ctrl + D
or Ctrl + Z
? Did any of those terminate the loop?– TuanDT
Nov 26 '18 at 1:26
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10
|
show 3 more comments
3 Answers
3
active
oldest
votes
Are you sure you are hitting Ctrl-D
to send the EOF properly? The following code seems to work:
int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}
vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "n";
}
return 0;
}
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, usegetline
instead, as in this example.
– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
|
show 1 more comment
I have not yet learned about iterators. So I came up with the following solution:
I use a getline to take all input and place into a string variable. I then have a for loop run through it, building a temporary string as it goes, until it encounters a space. When it sees a space, it adds the temporary variable to the vector, and resets the temporary variable. It continues this way until it reaches the end of the string.
add a comment |
In an interactive console/compiler while(cin >> inputString)
will continue wait for user-input.
It may work on a non-interactive console/compiler that reads data from a static standard input. But it's worth noting that in (most conforming) interactive compilers, cin >> inputString
will continue to wait for user input, and will (should) not evaluate to false until there occurs an error in reading input.
You may want to signal the program that input is finished. One way of doing this is to provide a keyword such as EOF
which will break the while-loop (although the disadvantage of this is that you can't use EOF
in the content of your input).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473645%2fhow-to-read-cin-into-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you sure you are hitting Ctrl-D
to send the EOF properly? The following code seems to work:
int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}
vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "n";
}
return 0;
}
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, usegetline
instead, as in this example.
– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
|
show 1 more comment
Are you sure you are hitting Ctrl-D
to send the EOF properly? The following code seems to work:
int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}
vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "n";
}
return 0;
}
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, usegetline
instead, as in this example.
– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
|
show 1 more comment
Are you sure you are hitting Ctrl-D
to send the EOF properly? The following code seems to work:
int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}
vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "n";
}
return 0;
}
Are you sure you are hitting Ctrl-D
to send the EOF properly? The following code seems to work:
int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}
vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "n";
}
return 0;
}
answered Nov 26 '18 at 2:12
PaulPaul
3606
3606
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, usegetline
instead, as in this example.
– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
|
show 1 more comment
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, usegetline
instead, as in this example.
– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Pressing Ctrl-D stops the loop and causes the program to function normally. But why do I have to press Ctrl-D? Is there no other way to make the program detect the end of the input?
– Joshua Minett
Nov 26 '18 at 2:28
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
Somehow the user needs to tell the program that he is done entering input. Whether you use a newline ("n") or EOF (Ctrl-D) to do that is up to you. I suppose the relevant question is, what do you mean by "end of the input"?
– Paul
Nov 26 '18 at 2:32
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
The user will enter either a bunch of space-separated text or provide a file containing a bunch of space-separated text. The end of the input is when the last word has been read from the console or file and input into the word vector.
– Joshua Minett
Nov 26 '18 at 2:46
@JoshuaMinett Okay, if you only want to read one line of characters, use
getline
instead, as in this example.– Paul
Nov 26 '18 at 2:57
@JoshuaMinett Okay, if you only want to read one line of characters, use
getline
instead, as in this example.– Paul
Nov 26 '18 at 2:57
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
Trouble with that method is that it removes the word-by-word reading. I want a vector with a word in each item. The getline method would add the whole thing, instead of word by word.
– Joshua Minett
Nov 26 '18 at 3:10
|
show 1 more comment
I have not yet learned about iterators. So I came up with the following solution:
I use a getline to take all input and place into a string variable. I then have a for loop run through it, building a temporary string as it goes, until it encounters a space. When it sees a space, it adds the temporary variable to the vector, and resets the temporary variable. It continues this way until it reaches the end of the string.
add a comment |
I have not yet learned about iterators. So I came up with the following solution:
I use a getline to take all input and place into a string variable. I then have a for loop run through it, building a temporary string as it goes, until it encounters a space. When it sees a space, it adds the temporary variable to the vector, and resets the temporary variable. It continues this way until it reaches the end of the string.
add a comment |
I have not yet learned about iterators. So I came up with the following solution:
I use a getline to take all input and place into a string variable. I then have a for loop run through it, building a temporary string as it goes, until it encounters a space. When it sees a space, it adds the temporary variable to the vector, and resets the temporary variable. It continues this way until it reaches the end of the string.
I have not yet learned about iterators. So I came up with the following solution:
I use a getline to take all input and place into a string variable. I then have a for loop run through it, building a temporary string as it goes, until it encounters a space. When it sees a space, it adds the temporary variable to the vector, and resets the temporary variable. It continues this way until it reaches the end of the string.
answered Nov 26 '18 at 5:27
Joshua MinettJoshua Minett
233
233
add a comment |
add a comment |
In an interactive console/compiler while(cin >> inputString)
will continue wait for user-input.
It may work on a non-interactive console/compiler that reads data from a static standard input. But it's worth noting that in (most conforming) interactive compilers, cin >> inputString
will continue to wait for user input, and will (should) not evaluate to false until there occurs an error in reading input.
You may want to signal the program that input is finished. One way of doing this is to provide a keyword such as EOF
which will break the while-loop (although the disadvantage of this is that you can't use EOF
in the content of your input).
add a comment |
In an interactive console/compiler while(cin >> inputString)
will continue wait for user-input.
It may work on a non-interactive console/compiler that reads data from a static standard input. But it's worth noting that in (most conforming) interactive compilers, cin >> inputString
will continue to wait for user input, and will (should) not evaluate to false until there occurs an error in reading input.
You may want to signal the program that input is finished. One way of doing this is to provide a keyword such as EOF
which will break the while-loop (although the disadvantage of this is that you can't use EOF
in the content of your input).
add a comment |
In an interactive console/compiler while(cin >> inputString)
will continue wait for user-input.
It may work on a non-interactive console/compiler that reads data from a static standard input. But it's worth noting that in (most conforming) interactive compilers, cin >> inputString
will continue to wait for user input, and will (should) not evaluate to false until there occurs an error in reading input.
You may want to signal the program that input is finished. One way of doing this is to provide a keyword such as EOF
which will break the while-loop (although the disadvantage of this is that you can't use EOF
in the content of your input).
In an interactive console/compiler while(cin >> inputString)
will continue wait for user-input.
It may work on a non-interactive console/compiler that reads data from a static standard input. But it's worth noting that in (most conforming) interactive compilers, cin >> inputString
will continue to wait for user input, and will (should) not evaluate to false until there occurs an error in reading input.
You may want to signal the program that input is finished. One way of doing this is to provide a keyword such as EOF
which will break the while-loop (although the disadvantage of this is that you can't use EOF
in the content of your input).
edited Nov 26 '18 at 2:36
answered Nov 26 '18 at 2:17
TrebuchetMSTrebuchetMS
2,5431923
2,5431923
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473645%2fhow-to-read-cin-into-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How would the program know when the "writing sample" is complete? Does the user send an EOF from the terminal?
– Paul
Nov 26 '18 at 1:21
did you hit
Ctrl + D
orCtrl + Z
? Did any of those terminate the loop?– TuanDT
Nov 26 '18 at 1:26
@Paul The program would know the writing sample is complete when it attempts to cin and cannot because there is no more input.
– Joshua Minett
Nov 26 '18 at 1:42
@TuanDT Yes, Ctrl + D terminates the loop and causes it to output the expected results.
– Joshua Minett
Nov 26 '18 at 1:42
"wordVector is type string." emm... it's a vector of strings?
– TrebuchetMS
Nov 26 '18 at 2:10