I want to take input in form of : T test cases in first line then followed by an integer ‘n’ and then n strings which may or may not contain spaces. I tried it using getline but couldn’t succeed . Please help … One such sample for input is given below :
2
3
ADAM
BOB
JOHNSON
2
A AB C
DEF
so just make a structure or class and create a array of that
struct arrayofstring(){
char a[1000000000];
};
int main(){
arrayofstring b[10000000000];
}
@nikhil97645
Your Problem: When you read “ADAM” using geline() first time it will read successfully, But when loop repeats(like 2nd time) then it will take the blank line as input. That’s all the problem is.
Solution1: In the loop, just write getline(cin, str) 2 times [This will work if you provide input without any endline or Enter key press].
Solution2: Use a While loop which will read until you write at least 1 character:-
while (str.length()==0 )
getline(cin, str);
You should probably just post your code as using getline
is precisely the way to do this, so we need to see your code to see what it’s doing wrong.
Thanks everyone …
Here is my code , Please correct me abt where I am going wrong
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD (ll) (1e9+7)
int main()
{
int t;
cin>>t;
for(int tt= 1;tt<=t;tt++)
{
int n;
cin>>n;
string s;
set<pair<int,string>> se;
for(int i=0;i<n;i++)
{
set se1;
string pt;
cin.get();
getline(cin,pt);
cout<<pt<<"\n";
}
}
}
This could be solved like this:
int t,n;
int main() {
cin >> t;
while(t--) {
cin >> n;
for (int i=0; i<n; i++) {
string s;
getline(cin, s);
// Keep reading a new line while there is
// a blank line
while ((int) s.size() == 0)
getline(cin, s);
cout << s << "\n";
}
}
return 0;
}