Undeclared identifiers (Range-based for Loops)


Why does mosh’s code not report an error, but my code reports ch as an undefined identifier when auto ch?

#include<iostream>;

using namespace std;

int main() {
string name = "Code with mosh";
	/*auto ch=0;*/
	for (auto ch : name);
	cout << ch << endl;

	return 0;
}

1 Like

u have added a semicolon " ; " in the “for(auto ch:name)”

#include<iostream>;

using namespace std;

int main() {
string name = "Code with mosh";
	/*auto ch=0;*/
	for (auto ch : name){
	cout << ch << endl;
}
	return 0;
}

this code will work.
note that ‘;’ is used to denote the end of a code block

2 Likes

Great!! Error resolved!
Maybe I need to get in the habit of adding {}
Thank you so much!