36

Suppose I want to read line a of integers from input like this:

1 2 3 4 5\n

I want cin to stop at '\n' character but cin doesn't seem to recognize it.

Below is what I used.

vector<int> getclause() {
  char c;
  vector<int> cl;

  while ( cin >> c && c!='\n') {    
    cl.push_back(c);
    cin>>c;
  }
  return cl;
}

How should I modify this so that cin stop when it see the '\n' character?

1
  • 9
    cin is whitespace delimited, so any whitespace (including \n) will be discarded. Thus, c will never be \n.
    – crashmstr
    Commented Mar 12, 2012 at 19:45

6 Answers 6

44

Use getline and istringstream:

#include <sstream>
/*....*/
vector<int> getclause() {
  char c;
  vector<int> cl;
  std::string line;
  std::getline(cin, line);
  std::istringstream iss(line);
  while ( iss >> c) {    
    cl.push_back(c);
  }
  return cl;
}

4 Comments

Enter at least 15 characters
    Mark
I am getting variable ‘std::istringstream iss’ has initializer but incomplete type
Enter at least 15 characters
Enter at least 15 characters
getline is also in namespace std, I think.
Enter at least 15 characters
Enter at least 15 characters
@Mark: That means you need #include <sstream>. That message almost always means that a type has been declared but you're missing the actual definition. In other words, you're missing a header.
Enter at least 15 characters
Enter at least 15 characters
@Drew Dormann fixed, i missed that one :D. Regarding the std::istringstream error, just include sstream as you can see in the example code.
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters
5

You can read all whitespace by setting noskipws on the istream:

#include <ios>
#include <iostream>
#include <vector>

using std::vector;

vector<int> getc() {
    char c;
    vector<int> cl;
    std::cin >> std::noskipws;
    while (std::cin >> c && c != '\n') {
        cl.push_back(c);
        std::cin >> c;
    }
    return cl;
}

If the standard input contains only a single line, you might as well construct the vector with the istream_iterator:

#include <iostream>
#include <iterator>
#include <vector>

using std::vector;

vector<int> getc() {
    // Replace char with int if you want to parse numbers instead of character codes
    vector<int> cl{
        std::istream_iterator<char>(std::cin),
        std::istream_iterator<char>()
    };
    return cl;
}

Comments

Enter at least 15 characters
Enter at least 15 characters
2

You can use the getline method to first get the line, then use istringstream to get formatted input from the line.

Comments

Enter at least 15 characters
Enter at least 15 characters
1

Use std::getline, this will do the trick

Comments

Enter at least 15 characters
Enter at least 15 characters
0

getchar() is more efficient than cin when working with characters at this situation

I tried to do the same with a line of characters with unknown length and want it to stop at a newline but it has an infinite loop and haven't detect the newline, so I just used getchar() instead of cin and it works

Comments

Enter at least 15 characters
Enter at least 15 characters
-2

From this link, it is quite simple to achieve this.

#include <stdio.h>
int main(void) {
    int i=0,size,arr[10000];
    char temp; 
    do{
        scanf("%d%c", &arr[i], &temp); 
        i++; 
        } while(temp!= '\n');

    size=i; 
    for(i=0;i<size;i++){ 
        printf("%d ",arr[i]); 
    } 
    return 0;
}

1 Comment

Enter at least 15 characters
    jww
Sorry, C++, not C.
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.