我的程序收到错误"名称查找' a'因ISO而改为'范围"

时间:2015-11-12 14:26:11

标签: c++ error-correction

我'我正在制作程序来检查用户输入文本中的漏洞......漏洞就像是' A' B'' P'等......但它在循环中出现错误

我已在下面发布完整代码...帮助找到错误

       #include <iostream>
      #include <cstring>
      #ifdef __cplusplus__
        #include <cstdlib>
      #else
        #include <stdlib.h>
      #endif
      using namespace std;
      int main()
      {
          label:
          cout << "Enter Number of test Case : ";
          int tc;
          cin >> tc;
          int * hls = new int [tc];
          hls = {0};
          if(tc > 40)
          {
              if (system("CLS")) system("clear");
              goto label;
          }
          char *str = new char [tc];
          for(int a = 0; a < tc; ++a)
              {
              cout << "Enter your " << a+1 << "  text : ";
              cin >> str[a];
              }
      for(a = 0; a < tc; ++a) // getting error in this line.
       {
       for(int b = 0; b < strlen(str[a]); ++b)
        {
               switch(str[b])
               {
                   case 'A' :
                   case 'D' :
                   case 'O' :
                   case 'Q' :
                   case 'P' :
                   ++hls[b];
                   break;
                   case  'B' : hls[b] += 2;
                   break;
                   default :
                   break;
               }
           }
         }
     if (system("CLS")) system("clear");
      for(a = 0; a < tc; ++a)
         cout << hls[a] << endl;
    return 0;
   }

1 个答案:

答案 0 :(得分:2)

您的a未在第二个for循环范围内声明。

a仅存在于此for循环体内:

for(int a = 0; a < tc; ++a)
{
    cout << "Enter your " << a+1 << "  text : ";
    cin >> str[a];
}

现在a已不复存在,但您仍尝试使用它:

for(a = 0; a < tc; ++a) // getting error in this line.

(您也可以稍后再尝试使用它)

int a;的主体内声明main(),使其保持在范围内,或者(更好)在每个后续for循环中声明它,就像在第一个循环中一样。

代码还有其他问题,但那是你问过的问题。 :)

相关问题