2010-03-07 11 views
11

Questa è solo la prima parte dei miei compiti, ho corretto tutti gli altri errori di compilazione ma continuo a ricevere questo errore, ce ne sono cinque.Cercando di eseguire questo compito a casa, ma continuo a ricevere un errore di compilazione

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 

Ho provato a fare tutto con esso, ma probabilmente sto facendo qualcosa di sbagliato. Ovviamente sono io. Potrei usare un aiuto se non ti dispiace :). A proposito, se qualcuno si chiedesse, fare il 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 Si prega di non aggiungere tag ai compiti, è attualmente nella lista nera (leggi la descrizione del tag). – Tim

+0

@Tim Ho eseguito il rollback della modifica, che seguiva la descrizione del tag di non rimuovere a meno che la domanda non richiedesse la pulizia. Per essere onesti, lo avevo comunque aggiunto ad alcune nuove domande. – pickypg

+0

@Josh: dovresti cercare di mantenere costante il rientro.Potresti essere sorpreso di quanti errori possano essere evitati con il rientro corretto semplicemente perché vedi gli errori quando indentati, come le variabili dichiarate nella portata errata a seconda del caso. :) –

risposta

18

Dichiarare "j" all'esterno del ciclo "for". Quando lo dichiari all'interno dello l'intestazione del ciclo, è locale al blocco del ciclo e non è visibile al di fuori di esso.

3

Si sta impostando j = 0 senza dichiararlo come int j = 0.

avete fatto all'interno del ciclo for ma la sua portata locale, dura solo per il corpo del ciclo ..

11

Quando si dichiara una variabile all'interno di una dichiarazione for, quella variabile è soltanto una portata per il corpo di il ciclo for, ad es

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

// But j is undefined out here 
2

Quando si ha qualcosa come

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

i è disponibile solo nel campo di applicazione del ciclo for, quindi se si fa qualcosa di simile:

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

È possibile ottenere una compilation errore a cout << i;, perché i non esiste più dopo il ciclo for terminato.

1

La variabile j è locale al ciclo for. È necessario aumentare la portata, facendo qualcosa di simile a questo:

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

o ridichiarare in un secondo momento:

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

j esiste solo nel ciclo for, che è in istruzioni

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; 

Avresti dovuto scrivere

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

In C++, l'ambito di una variabile dichiarata in un ciclo for è il ciclo. Quindi quando dici:

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

La variabile j esiste solo nel corpo del ciclo.

Problemi correlati