2010-03-07 42 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只存在于循环体中。

相关问题