【问题标题】:My Program is getting an error "name lookup of 'a' changed for ISO 'for' scoping "我的程序收到错误“'a' 的名称查找已更改为 ISO 'for' 范围”
【发布时间】:2015-11-12 14:26:10
【问题描述】:

我正在制作程序来检查用户输入文本中的孔...孔是像'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;
   }

【问题讨论】:

标签: c++ error-correction


【解决方案1】:

您的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.

(你也试试以后再用)

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

代码还有其他问题,但这是您询问的问题。 :)

【讨论】:

  • 感谢它的工作,但在下一行得到另一个
  • 是的,但 SO 更喜欢具体问题,而不是交互式“调试我的代码”会话。您在下一行的错误是因为 strtc 字符的单个字符数组,但您试图将其索引为数组数组。您使用 C 风格的字符串和内存管理有什么特别的原因吗?使用std::stringstd::vector 编写代码会容易得多。
猜你喜欢
  • 2014-12-20
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2021-10-27
相关资源
最近更新 更多