c++ - How to input n lines in a string array? -
this question exact duplicate of:
i have input int n, , read n lines in string array. when test code, example, put 3, read 2. found should use vectors, why, there way easier vectors read n lines ?
example code:
#include <iostream> using namespace std; int main() { int n; cin >> n; string niz[n]; (int t1 = 0; t1 < n; t1++) { getline(cin, niz[t1]); } (int t2 = 0; t2 < n; t2++) { cout << niz[t2] << endl; } }
the problem when read number of lines newline still left in stream, first line read empty line (what remains after number input).
see , example exchange of input , output when modify program little prefix each line of output:
c:\so-test>test 3 mary had little lamb. line [0]: line [1]: mary had line [2]: little lamb.
for moment, i'll leave solution exercise reader.
Comments
Post a Comment