holes-not able to find what's wrong with my code

#include
#include<stdio.h>

char b[100];
using namespace std;

int main()

{
int a,t=0,c[40],s;
cin>>a;

do
{
s=0;
gets(b);

for(int i=0;b[i]!=’\0’;i++)
{
if(b[i]==‘A’||b[i]==‘D’||b[i]==‘O’||b[i]==‘P’||b

[i]==‘R’||b[i]==‘Q’)
s+=1;
if(b[i]==‘B’)
s+=2;
}

c[t]=s;
t++;
}while(t<a);

for(int J=0;J<a;J++)
cout<<c[J]<<endl;

return(0);

}

Your code is not passing even sample test cases. See here, your way of taking input wasn’t correct.
Whenever you input a, there also a “\n” character left by cin. cin and cout flushes \n character in the input stream. Therefore whenever you use gets end of file indicator is set(feof). Hence, the pointer returned is a null pointer and the contents of b remain unchanged. Same is the case when you take input using getline(). Hence, use cin.

1 Like

your input method is wrong

here is your corrected code
ideone link

first thing the problem is with ‘\n’ due to which your code is unable to get the desired string…

so in modified code i have added getchar() after testcase input,

Secondly, i have made some changes after which your code will print answer after each input.

After all these changes i have submitted the modified code to check whether it passes all the CC cases or not and your code got AC.

You may check the submission here:
http://www.codechef.com/viewsolution/5642260