For loop isn't working C++ -
#include <iostream> using namespace std; int main() { char ar[4][30]; int i=0,t; cout << "enter test cases"<< endl; cin>>t; for(i=0;i<t;i++) { gets(ar[i]); } return 0; }
from code want enter multiple strings. let's come problem, when enter number of test cases, ar[i] accepts 1 string less test cases.i don't understand why code isn't working. working on eclipse.
when cin >> t
, there's linefeed left in input buffer that's being read, , fed firsr gets
invocation.
assume user input:
2 aaa bbb
after cin >> t
, input buffer is
<enter> aaa bbb
so first time call gets
, reads enter , returns, ar[1]
set empty string.
you add cin.ignore()
or gets
discard before enter (including).
cin >> t; -> gets(null);
Comments
Post a Comment