2010-03-07 14 views
11

Esta es solo la primera parte de mi tarea, he solucionado todos los otros errores de compilación pero sigo recibiendo este error, hay cinco.Intentando hacer esta tarea pero sigo recibiendo un error de compilación

1>\takehome\main.cpp(39) : error C2065: 'j' : undeclared identifier 
1>\takehome\main.cpp(44) : error C2065: 'j' : undeclared identifier 
\takehome\main.cpp(45) : error C2065: 'j' : undeclared identifier 
\takehome\main.cpp(76) : error C2065: 'j' : undeclared identifier 
\takehome\main.cpp(80) : error C2065: 'j' : undeclared identifier 

He intentado hacer todo lo posible con él, pero probablemente estoy haciendo algo mal ... Obviamente lo estoy. Podría usar algo de ayuda si no te importa :). Por cierto, en caso de que alguien se esté preguntando, haciendo Simpletron.

#include <iostream> 
using namespace std; 

int main() 
{ 
int memory[100]; //Making it 100, since simpletron contains a 100 word mem. 

int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized. 

int operand; 

int accum = 0; // the special register is starting at 0 

int position = 0; //making the starting position to be 0. 

for (int j = 0; j < 100; j++) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j. 

    memory[j] = 0; 


// This is for part a, it will take in positive variables in a sent-controlled loop and compute + print their sum. These are random variables. 
memory [0] = 2942; 

memory [1] = 2342; 

memory [2] = 3523; 

memory [3] = 2031; 

memory [4] = 5000; 

memory [5] = 8080; 

memory [6] = 3425; 

j = 0; //Makes the variable j start at 0. 

while (true) 
{ 

    memory[ j ]%100 = operand; // Finds the op codes from the limit on the memory (100) 
    memory[ j ]%100 = operation; 

    //using a switch loop to set up the loops for the cases 
    switch (operation){ 
    case 1: //reads a variable into a word from loc. 
    cout <<"\n Input a positive variable: "; 
    cin >> memory[ operand ]; break; 

    case 2: // takes a word from location 
    cout << "\n\nThe content at location " << operand << "is " << memory[operand]; break; 

    case 3:// loads 
    accum = memory[ operand ]; break; 

    case 4: //stores 
    memory[ operand ] = accum; break; 

    case 5: //adds 
    accum = accum + memory[ operand ]; break; 


    case 6: // subtracts 
    accum = accum - memory[ operand ]; break; 

    case 7: //divides 
    accum = accum/(memory[ operand ]); break; 

    case 8: // multiplies 
    accum = accum*memory [ operand ]; break; 

    case 9: // Branches to location 
    j = -1; break; 

    case 10: //branches if acc. is < 0 
    if (accum < 0) 
    j = 5; break; 

    case 11: //branches if acc = 0 
    if (accum == 0); break; 

    case 12: // Program ends 
    exit(0); break; 
} 
j++; 
} 
return 0; 
} 
+0

@pickypg Por favor, no agregue la etiqueta de tarea a las preguntas, actualmente está en la lista negra (lea la descripción de la etiqueta). – Tim

+0

@Tim Revertí el cambio, que estaba siguiendo la descripción de la etiqueta de no eliminar a menos que la pregunta necesitara una limpieza. Aunque para ser justo, todavía lo estaba agregando a algunas preguntas nuevas. – pickypg

+0

@Josh: debe esforzarse por mantener la indentación constante.Puede sorprenderse cuántos errores se pueden evitar con la sangría correcta simplemente porque ve los errores cuando sangra, como las variables declaradas en el ámbito incorrecto según el caso. :) –

Respuesta

18

Declare "j" fuera del bucle "for". Cuando lo declara dentro de el encabezado de bucle, es local al bloque del bucle y no es visible fuera de él.

3

Está configurando j = 0 sin declararlo como int j = 0.

Usted lo hizo dentro del bucle for pero su alcance local dura sólo por el cuerpo del bucle ..

11

Cuando se declara una variable dentro de una declaración for, esa variable es única en su alcance para que el cuerpo de el bucle for, por ejemplo

for (int j = 0; j < 100; j++) 
{ 
    // j is defined in here 
} 

// But j is undefined out here 
2

Cuando usted tiene algo así como

for (int i = 0; i < k; ++i) 
{ 
// stuff 
} 

i sólo está disponible en el alcance del bucle for, así que si haces algo como esto:

for (int i = 0; i < k; ++i) 
{ 
// stuff 
} 
cout << i; 

Usted recibe una compilación error en cout << i;, porque i ya no existe después de terminar el ciclo for.

1

La variable j es local al bucle for. Es necesario aumentar su alcance, haciendo algo como esto:

int j; 
for(j = 0; j < 100; ++j) 

o redeclare más tarde:

for(int j=0; j<100; ++j) 
... 
... 
int j = 0; 
while(true) 
... 
1

j existe sólo en el bucle, es decir en la instrucción

for (int j = 0; j < 100; j++) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j. 
    memory[j] = 0; 

Deberías haber escrito

int j; 
for(j=0;j<100;j++) 
... 
2

En C++, el alcance de una variable declarada en un ciclo for es el ciclo. Por lo tanto, cuando diga:

for (int j = 0; j < 100; j++) { 
    // j only exists here (and in the for statement itself) 
} 

La variable j solo existe en el cuerpo del bucle.

Cuestiones relacionadas