Getline not working

hey everyone
when I use getline with strings, it doesn't work. In my code, I am creating a string array called crew. The user inputs the number of strings in this array.
My code:
#include <iostream>
#include <string
using namespace std;
int main() {
int n = -11;
while ( n < 1 || n > 100)
{
cout << "PLEASE ENTER: ";
cin >> n;
}
string crew[n];
for (int i = 0; i < n; i++) {
getline(cin,crew[i]);
}
for (int i = 0; i < n; i++) {
cout << crew[i] << " ";
}
return 0;
}
If I chose 1 for the number, the program will terminate immediately. If I choose two, the program won't terminate but will take only one input and terminate
Please help this is part of a bigger, longer program
Thanks
when I use getline with strings, it doesn't work.

Actually it appears to be working correctly.

Do you know that getline(), by default, stops processing a string when it encounters a new line character?

Do you know that cin leaves the new line character in the input buffer?



@jlb
thank you for replying
No, it is not working correctly - try to copy-paste it on your compiler and try inputting two values and the program will terminate after you enter the first
What is a new line character?
Jack Van Stone wrote:
try to copy-paste it on your compiler and try inputting two values
If you PUT IT IN CODE TAGS we wouldn't have to copy-paste anything.


What is a new line character?

'\n';
Signifies the end of a line.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   int n = -11;
   while ( n < 1 || n > 100)
   {
      cout << "How many in the crew? ";
      cin >> n;
   }
   cin.ignore( 1000, '\n' );       // clear that newline
   
   vector<string> crew(n);         // VLAs aren't allowed
   for (int i = 0; i < n; i++) 
   {
       getline(cin,crew[i]);
   }
   
   for (int i = 0; i < n; i++)
   {
       cout << crew[i] << "  ";
   }
   cout << '\n';
}


How many in the crew? 3
David
Thomas
Stephen
David  Thomas  Stephen 


Last edited on
Hey, Jack Van Stone, as a friendly piece of advice:

It is not usually very productive to claim that some piece of the C++ language is not functioning properly, and to immediately discount the advice given you in responses.

The way that formatted input behaves is different than unformatted input, and this particular question is so common among new C++ learners that your question title is almost a stock phrase.

You didn't know any of that, of course, but consider that most of us responding here are doing it simply because we want to help you learn, and no other reason. Even the more curmudgeonly among us aren't interested in mistreating you, just redirecting you with information you need to know.

Hope this helps.
while '\n' is the 'new line character' when writing code to output a new line, another new guy trap: the actual new line in raw bytes is often 2 characters. You don't have to DO anything about this right now, but be aware of it.
The actual newline sequence depends on your OS/compiler. For Windows, its '\r\n'. For everything else its '\n'. (For ancient Macs its '\r', and for some old mainframes it varies.)

Translation between <whatever> and '\n' is what “text mode” is all about; “binary mode” does not do the translation.
Topic archived. No new replies allowed.