HOLES problem

#include
using namespace std;
int main()
{
int test;
int i,j,hole=0;
char str[100];
cin>>test;

    for(i=0;i<test;i++)
    {
                       gets(str);
                       for(j=0;str[j]!='\0';j++)
                       {
                                                if(str[j]=='Q'||str[j]=='R'||str[j]=='O'||str[j]=='P'||str[j]=='A'||str[j]=='D')
                                                hole++;
                                                else
                                                if(str[j]=='B')
                                                hole=hole+2;
                       }
                       cout<<hole<<endl;
                       
                  
    }
}

why this program is printing 1st result as 0ā€¦ and if i check it for ā€˜nā€™ inputsā€¦ it run only for 'n-1ā€™timesā€¦
1st result always prints 0ā€¦ rest all are correctā€¦ someone plzz helpā€¦

I saw such problem before, if I remember well, ā€œproblemā€ is in

cin>>test;

statement that does not read '\n', add gets(str); before for statement :wink:

2 Likes

#include<stdio.h>

int main()
{
	int t,count, i, j;
	char str[100] ;
	scanf("%d",&t) ;
	for(i=0;i<t;i++)
	{
		scanf("%s",&str) ;
		count=0;
		for(j=0;str[j]!='\0';j++)
		{
			if(str[j] == 'A' || str[j] == 'D' || str[j] == 'O' || str[j] == 'P' || str[j] == 'Q' || str[j] == 'R')
				count++ ;
			else if(str[j] == 'B')
				count += 2 ;
		}
		printf("%d\n",count);
	}
	return 0 ;
}
4 Likes