I, of course, are not the most experienced programmer, but why after compiling this code:

#include <iostream> #include <string> using namespace std; int main(int argc, char** argv) { string s; cin >> s; int N = 0; for ( int i = 0 ; i < s.size() ; i++ ){ if (s[i] != '_') N++; } string mas[N]; int i = 0; int j = 0; int d = 1; while ( i < N ){ while (s[j] != '_' && j < s.size()){ if ( s[j] != s[d] ) int e = d; mas[i] = mas[i] + s[j]; d++; if ( d == '_' ){ d = e + 1; } } i++; j++; } cout << mas[i]; return 0; } 

Dev produces this error: [Error] 'e' was not declared in this scope

Closed due to the fact that it was off topic by Nicolas Chabanovsky 22 May '16 at 17:23 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I look, the exact answer is not given. In short, the scope of a variable declared in an if is limited to this instruction, so that the declaration int e = d ceases to be visible already in the next line. - Harry

3 answers 3

This

  if ( s[j] != s[d] ) int e = d; mas[i] = mas[i] + s[j]; 

actually equal to

  if ( s[j] != s[d] ){ int e = d; } mas[i] = mas[i] + s[j]; 

What is checked here?

     while (s[j] != '_' && j < s.size()){ if ( s[j] != s[d] ) int e = d; 

    If s[j] = s[d] , then e will not be declared. Declare е outside the loop and in this place do the assignment.

    • It does not matter whether it will be announced or not. The scope of e here is the body of if and nothing more. - Harry
     #include <iostream> #include <string> using namespace std; int main(int argc, char** argv) { string s; cin >> s; int N = 0; for ( int i = 0 ; i < s.size() ; i++ ){ if (s[i] != '_') N++; } string mas[N]; int i = 0; int j = 0; int d = 1; int e = 0; //объявим как все остальные переменные while ( i < N ){ while (s[j] != '_' && j < s.size()){ if ( s[j] != s[d] ) mas[i] = mas[i] + s[j]; d++; if ( d == '_' ){ d = e + 1; } } i++; j++; } cout << mas[i]; return 0; } 

    the program will go into an infinite loop when working, of course