I dont understand the code runs fine on viusal studio (easy problem "HOLES")

//compilation on code shef
prog.cpp: In function ‘int main()’:
prog.cpp:16:7: error: expected ‘(’ before ‘each’
for each (char c in s)
^
prog.cpp:16:13: error: expected primary-expression before ‘char’
for each (char c in s)
^
prog.cpp:16:24: error: ‘each’ was not declared in this scope
for each (char c in s)
^
prog.cpp:17:3: error: expected ‘;’ before ‘{’ token
{
^
prog.cpp:52:1: error: expected primary-expression at end of input
}
^
prog.cpp:52:1: error: expected ‘;’ at end of input
prog.cpp:52:1: error: expected primary-expression at end of input
prog.cpp:52:1: error: expected ‘)’ at end of input
prog.cpp:52:1: error: expected statement at end of input
prog.cpp:52:1: error: expected ‘}’ at end of input
prog.cpp:8:6: warning: unused variable ‘count’ [-Wunused-variable]
int count = 0;
^
prog.cpp:52:1: error: expected ‘}’ at end of input
}
^
//My Program
#include
#include
using std::string;
int main()
{

int t;
int count = 0;
std::cin >> t;
string s;

while (t != 0)
{
	
	std::cin >> s;
	for each (char c in s)
	{
		if (c == 'A')
		{
			count++;
		}
		else if (c == 'D')
		{
			count++;
		}
		else if (c == 'O')
		{
			count++;
		}
		else if (c == 'P')
		{
			count++;
		}
		else if (c == 'R')
		{
			count++;
		}
		else if (c == 'Q')
		{
			count++;
		}
		else if (c == 'B')
		{
			count += 2;
		}

	}
	std::cout << count;
	count = 0;
}
return 0;

}

I never used for each loop in C++, but got to see when I saw your code. This is what I got, the newer versions of C++ allows the usage of for each loop on standard library elements. Everywhere I got to see the loops like this:

for(char &c: str)  // for input string str, lets say abcd
 cout<<c<<" ";     // output will be a b c d

I didn’t get to see the usage of for each loop the way you have mentioned but as you have said, the code works fine on visual studio, then it must surely be because of incompatibility. Even usage of above loop gives this error in C++ 4.9.2:

prog.cpp: In function 'int main()':
prog.cpp:16:19: error: range-based 'for' loops are not allowed in C++98 mode
 for (char& c: s)

Then I ran the code in C++14, and the following code works fine, also there was a logical error in your code that you are not decrementing the variable t which leads to RTE. The corrected code is:

#include<iostream>
#include<string>

using std::string; 
using namespace std;

int main() {

int t;
std::cin >> t;
string s;

while (t != 0)
{
    int count = 0;
    std::cin >> s;
    for (char& c: s)
    {
        if (c == 'A')
        {
            count++;
        }
        else if (c == 'D')
        {
            count++;
        }
        else if (c == 'O')
        {
            count++;
        }
        else if (c == 'P')
        {
            count++;
        }
        else if (c == 'R')
        {
            count++;
        }
        else if (c == 'Q')
        {
            count++;
        }
        else if (c == 'B')
        {
            count += 2;
        }

    }
    std::cout << count<<endl;
    count = 0;
    t--;
}
return 0;

}