2010-03-07 44 views
11

這只是我的作業的第一部分,我已經修復了所有其他編譯錯誤,但我不斷收到此錯誤,theres五。試圖做這個家庭作業,但我不斷收到編譯錯誤

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 

我試過用它做所有事情,但我可能做錯了什麼事情。顯然我是。如果你不介意的話,我可以使用一些幫助:)。順便說一句,如果有人想知道,做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請不要將作業標籤添加到問題中,它目前被列入黑名單(閱讀標籤說明)。 – Tim

+0

@Tim我回滾了這個變化,這是在標籤的描述中沒有移除,除非問題需要清理。儘管公平,但我仍然將它添加到一些新的問題。 – pickypg

+0

@Josh:你應該努力保持縮進一致。您可能會驚訝地發現,通過正確縮進可以避免多少錯誤,因爲您在縮進時會看到錯誤,例如在此處顯示的錯誤範圍中聲明的變量。 :) –

回答

18

在「for」循環之外聲明「j」。當你在裏面聲明這個循環頭時,它在循環的塊中是本地的,並且在它之外不可見。

3

您正在設置j = 0,但未聲明它爲int j = 0

你做這件事的for環內,但其本地範圍只持續了循環體..

11

在聲明for聲明的內部變量,該變量僅在範圍爲主體例如,for循環

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

// But j is undefined out here 
2

當你有像

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

i只在for循環的範圍內都有效,所以如果你做這樣的事情:

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

你得到一個編譯cout << i;錯誤,因爲for循環完成後i不再存在。

1

變量j是for循環的局部變量。你需要增加它的範圍,通過做這樣的事情:

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

或更高版本重新聲明它:

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

j只存在於for循環,也就是在指令

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; 

您應該寫下

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

在C++中,for循環中聲明的變量範圍是循環。所以當你說:

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

變量j只存在於循環體中。

相關問題