Go Down

Topic: crosses initalization of? (Read 5102 times) previous topic - next topic

TaeYeonKim

it`s my code but it doesn`t work
Code: [Select]

#include <IRremote.h>
#include <IRremoteInt.h>
#include <IRremoteTools.h>
#include <SevSeg.h>


SevSeg sevseg;
int Time = 0;
int Min = 0;
int Sec = 0;
int StartStopValue = 0;
int RecvPin = 11;
IRrecv irrecv(RecvPin);
decode_results results;

void setup() {
  irrecv.enableIRIn();
  sevseg. Begin(0,0,1,2,3,4,5,6,7,8,9,10,13);
  sevseg. Brightness(90);
}

void loop() {
  // put your main code here, to run repeatedly:
  while(StartStopValue == 0) {
     switch(results.value) {     
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
     
      if (irrecv.decode(&results)) {
      long int decCode = results.value;
     
      case 16724175: {
      Min = Min + 30;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16718055: {
      Min = Min + 10;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16743045: {
      Min = Min + 5;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16716015: {
      Min = Min + 3;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16726125: {
      Min = Min + 1;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16734885: {
      Sec = Sec + 30;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16728765: {
      Sec = Sec + 10;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16730805: {
      Sec = Sec + 5;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16732845: {
      Sec = Sec + 1;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16738455: {
      Min = 0;
      Sec = 0;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     case 16761405: {
      StartStopValue = 1;
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
     default: {
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
      break;
     }
     
    }
   
     if(Min > 99); {
      Min = 0;
     }
     
     if(Sec > 59); {
      Sec = 0;
     }
     
     Time = Min * 100 + Sec;

     sevseg.NewNum(Time,2);
     sevseg.PrintOutput();
     if (irrecv.decode(&results)) {
     long int decCode =  results.value;
    }
   }
  }
  while(StartStopValue == 1); {
   if(Time % 100 != 0); {
    Time = Time - 1;
    delay(1000);
    sevseg.NewNum(Time,(byte) 2);
    sevseg.PrintOutput();
    }
   if(Time % 100 == 0); {
    Time = Time - 41;
    delay(1000);
    sevseg.NewNum(Time,(byte) 2);
    sevseg.PrintOutput();
   }
   if(results.value = 16761405); {
    StartStopValue = 0;
   }
   
   if(Time == 0); {
    StartStopValue = 0;
   }
  }
}




problem is "crosses initialization of 'long int decCode' " at 32nd line
(↓last line)

Code: [Select]

void loop() {
  // put your main code here, to run repeatedly:
  while(StartStopValue == 0) {
     switch(results.value) {     
      sevseg.NewNum(Time,2);
      sevseg.PrintOutput();
     
      if (irrecv.decode(&results)) {
      long int decCode = results.value;


what`s my wrong & how can fix it?

vaj4088

I do not know what the error message means and the error message is not properly posted.

That is a very strange switch/case statement.  The lines after the switch statement should be in a case statement but are not.  The if statement includes case statements but should not.  I want to help you to correct the errors but I do not understand what was intended so I cannot help further.

Good Luck!


groundFungus

#2
Oct 29, 2016, 05:01 pm Last Edit: Oct 30, 2016, 12:25 am by groundfungus
long int decode = results.value is a variable declaration.  You have that line twice within the same function line 31 and line 132.  You are declaring an already declared variable, that is what the compiler is complaining about.  Lose the long int in front of line 132 or use a different variable name (that has not been declared already).

posted by vaj4088
Quote
That is a very strange switch/case statement.
I agree with that.


I don't think that declaring a variable inside of a switch block is legal, now that I think about it.  I know for sure that you can't declare a variable in a case.

TaeYeonKim

So, put in it where?

Code: [Select]

if (irrecv.decode(&results)); {
long int decCode = results.value;

groundFungus

#4
Oct 30, 2016, 12:14 pm Last Edit: Oct 30, 2016, 01:22 pm by groundfungus
I am kind of guessing here, but try removing the long int from the second declaration so it becomes an assignment not a declaration.  I can't try it as I don't have the proper libraries installed.


Since reading Ray's post (scoping reminder), I realized that there is no duplicate definition as both declarations are in if statements so are local to the if block.  So I don't know how to fix the error.  Sorry for leading down the wrong path.

RayLivingston

I don't think that declaring a variable inside of a switch block is legal, now that I think about it.  I know for sure that you can't declare a variable in a case.
It's so often the things people are most sure of that are most un-true....  You absolutely can declare variables within a case.  You simply have to use curly braces to enclose the case so the scope of the variable is restricted to that case, which is precisely the error the OP is seeing.

Code: [Select]

switch (x)
{
    case 0:
       int i = 1;  // this is not legal
       break;
    
    case 1:
    {
       int i = 1;  // this is perfectly legal
       break;
    }
}


Regards,
Ray L.

groundFungus

I stand corrected and that is good to know.  

sterretje

So, put in it where?

Code: [Select]

if (irrecv.decode(&results)); {
long int decCode = results.value;

Before the switch. I just encountered the same error message (and a whole lot more) when I accidentally had a variable declaration / initialisation as the first few lines directly after the switch :(
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

lloyddean

Remove the ; near the end of the 'if', from ...

Code: [Select]

if (irrecv.decode(&results)); {
    long int decCode = results.value;


... to ...


Code: [Select]

if (irrecv.decode(&results))
{
    long int decCode = results.value;



vaj4088

There are at least three 'if's and a 'while' like this.


lloyddean

#10
Oct 30, 2016, 07:43 pm Last Edit: Oct 30, 2016, 08:39 pm by lloyddean
I thought perhaps pointing out that instance would get the rest of the code reviewed for similar or other such instances.

EDIT: Note the code has another issue in that it has a code declared inside the switch but outside an 'case' statement.

vaj4088

lloyddean wrote (in part):

Quote
I thought perhaps pointing out that instance would get the rest of the code reviewed for similar or other such instances.
It should.  I know that I would.  I am certain that you would.  However, I have seen cases on this forum in the past where that is not true.  Sigh.   :(

lloyddean wrote (in part):

Quote
Note the code has another issue in that it has a code declared inside the switch but outside an 'case' statement.
I already wrote
Quote
The lines after the switch statement should be in a case statement but are not
so I agree completely.


PaulMurrayCbr

For fun abuse of the C/C++ switch statement, see "Duff's Device".

http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up