Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ for循环作用域问题_C++_Arduino - Fatal编程技术网

C++ for循环作用域问题

C++ for循环作用域问题,c++,arduino,C++,Arduino,我使用的代码如下所示: const int NUMBER_OF_FIELDS = 3; int fieldIndex = 0; int values[NUMBER_OF_FIELDS]; void setup() { Serial.begin(9600); } void loop() { if(Serial.available()) { char ch = Serial.read(); if(ch>='0' && ch

我使用的代码如下所示:

const int NUMBER_OF_FIELDS = 3;
int fieldIndex = 0;
int values[NUMBER_OF_FIELDS];

void setup()
  {
  Serial.begin(9600);
  }

void loop()
  {
   if(Serial.available())
    {
     char ch = Serial.read();
     if(ch>='0' && ch <= '9')
       {
       values[fieldIndex] = (values[fieldIndex]*10 +(ch-'0'));
       }
      else if (ch == ',')
       {
        if(fieldIndex < NUMBER_OF_FIELDS -1)
        fieldIndex++;
       }
      else 
       {
       Serial.print(fieldIndex+1);
       Serial.println("fields recieved:");
       for (int i = 0; i<=fieldIndex; i++);
         {
         //Serial.println(values[i]);
         //values[i]= 0;
         }
        fieldIndex = 0; 
       } 
    } 
  }
const int NUMBER_OF_FIELDS=3;
int fieldIndex=0;
int值[字段的数量];
无效设置()
{
Serial.begin(9600);
}
void循环()
{
if(Serial.available())
{
char ch=Serial.read();

如果(ch>='0'&&ch在for后面有一个分号,那么只有for循环作用域的int i在for循环之外无效

你可能打算做以下事情

  for (int i = 0; i<=fieldIndex; i++)     // no semicolon
  {
     Serial.println(values[i]);
     values[i]= 0;
  }

for(int i=0;i
for(int i=0;i用
关闭了for循环的问题;

for (int i = 0; i<=fieldIndex; i++); // <-----

for(int i=0;i问题在于,您的
for
循环什么都不做:循环体在循环后面的分号处结束:

for (int i = 0; i<=fieldIndex; i++);
                                   ^ here

for(inti=0;我不担心我花了5分钟才注意到
for (int i = 0; i<=fieldIndex; i++);
                                   ^ here