Question- piyush and magical park ( failing 2 test cases out of 5 , not able to find my mistake need help)

#include
#include
using namespace std;
int flag=1;
int str;
void answer(string a,int t)
{
for(int i=0;i<a.length();++i)
{

if(i==a.length()-1&&a[i]!='#')
{   ++str;  }	

if(a[i]==’.’)
{ str=str-3;

if(str<t)
{ flag=0;
break; }

}
if(a[i]==’*’)
{ str=str+4; }
if(a[i]==’#’)
{
break; }

}
}
using namespace std;
int main() {
int n;
int m;
int k;
cin>>n>>m;
cin>>k>>str;
cin.get();
string s[100];
if(str<k)
{ cout<<β€œNo”;
return 0; }

for(int i=0;i<n;++i)
{

 getline(cin,s[i]);
 answer(s[i],k);
 if(flag==0)
 {   break;}

}

if(flag==0)
{
cout<<β€œNo”;
}
else if(flag==1)
{ cout<<β€œYes”<<endl;
cout<<str; }

return 0;

}

you have to check if you have enough energy to go to the next block as well, if you dont then you cannot exit the maze