c++ - How can i read characters through an array of string? -
i want 2 things in question, first code give me error: "string subscript out of range" , can't fix it!. second want me searching in array of string , read characters of each string.
this problem: the problem
test case:
2
5 ah
ibrahim ahmed
ahsan nas
ahmed hossam
ahmed
menan bad
2 fou
ayan
soli
output:
2
ahmed
ahsan nas
conan's assumption wrong!
my code:
#include<iostream> #include<string> using namespace std; int main() { short int t,n; cin>>t; string p,s[51],res; (int = 0; < t; i++) { int c=0,k=0; cin>>n; cin>>p; (int j = 0; j < n ; j++) { getline(cin,s[j]); } (int j = 0; j < n; j++) { (int z = 0; z < p.length(); z++) { cout<<s[j][z]<<" " <<endl; if (p[z]==s[j][z]) { c++; } } cout<<c; if (c==p.length()) { k++; } } if (k!=0) { cout<<k<<endl; c=0; (int j = 0; j < n; j++) { (int z = 0; z < p.length(); z++) { if (p[z]==s[j][z]) { c++; } } if (c==p.length()) { cout<<s<<endl; } } } else if (k==0) { cout<<"conan's assumption wrong!"<<endl; } } return 0; }
thanks in advance
around here:
cin>>p; (int j = 0; j < n ; j++) { getline(cin,s[j]); }
note `cin >> p' still leaves room in buffer. when enter loop, waste iteration clearing rest of space in previous line. when enter nth person, end index out of bounds exception. easy fix burn rest of line.
getline(cin, p); (int j = 0; j < n ; j++) { getline(cin,s[j]); }
should fix error, anyway! hope helps!
Comments
Post a Comment