Compiler version is 4.2 g++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a[10];
int i;
int N;
cin>>N;
for (i=0; i<N; i++)
{
getline(cin,a[i]);
}
return 0;
}
When I input 2 . It asks for input once. When 3 then 2 times . And so on. Please solve. THANKS.
The first getline
call reads end-of-line
character that is still sitting in the input buffer after the N is read.
Consider this as the following input:
3 First string
Second string
Third string
In your case, the first string is just empty.
If you want to ignore whitespaces after the N
, write something like
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
(will skip to the end of line), or
cin >> N >> std::ws;
(will skip all whitespace characters, end of line included).